473,796 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about user controls and DataGrids

I am pretty new to C# so bare with me...

The design of my website utilizes tables on every page that look almost
identical, save the data they are displaying. I have written the code
to create the body of these tables using a DataGrid, so I can
dynamically add rows to the table without modifying the HTML. Now, I
want to put this DataGrid into a user control that I can include in the
HTML code, then control the behavior of that DataGrid through functions
in the user control from the code-behind in the including page. Make
sense so far? I will try to explain a little better through an
example:

I have a page, user.aspx, that displays information about a user in
Active Directory in a table that is formatted to fit the design of the
website:

<table>
<tr>
<td>
Username
</td>
<td>
(...display username...)
</td>
</tr>
....
</table>

Now I want to pull the HTML out of the user.aspx page and put it into a
control called DefaultDisplayT able.ascx that will be included in the
HTML of the user.aspx page:

<ctrl:DefDispTb l id="tblDisplay " runat="server"> </ctrl:DefDispTbl >

In the DefaultDisplayT able.ascx, I create a DataGrid called grdUser:

<asp:datagrid id="grdUser" runat="server" datasource="<%# loTable%>"
autogeneratecol umns="False">
<columns>
<asp:boundcolum n datafield="colL abel"> </asp:boundcolumn >
<asp:boundcolum n datafield="colD ata"> </asp:boundcolumn >
</columns>
</asp:datagrid>

Then in the code-behind of this control, DefaultDisplayT able.ascx.cs, I
define a DataTable, create it's columns, etc... through a contructor
called from the code-behind of the user.aspx page, user.aspx.cs, as
well as create functions used to add rows to the table and bind the
table to the DataGrid:

public class DefaultDisplayT able : System.Web.UI.U serControl
{
protected System.Web.UI.W ebControls.Data Grid grdUser;
protected DataTable loTable;
public DefaultDisplayT able()
{
loTable = new DataTable("Data Table");

DataColumn dcLabel = new DataColumn();
dcLabel.DataTyp e = System.Type.Get Type("System.St ring");
dcLabel.ColumnN ame = "colLabel";
loTable.Columns .Add(dcLabel);

DataColumn dcData = new DataColumn();
dcData.DataType = System.Type.Get Type("System.St ring");
dcData.ColumnNa me = "colData";
loTable.Columns .Add(dcData);
}
public void Add_Table_Row(s tring lsLabelText, string lsDataText)
{
DataRow drLabel;
drLabel = loTable.NewRow( );
drLabel["colLabel"] = lsLabelText;
loTable.Rows.Ad d(drLabel);

DataRow drData;
drData = loTable.NewRow( );
drData["colData"] = lsDataText;
loTable.Rows.Ad d(drData);
}
public void Bind_DataTable( )
{
grdUser.DataBin d();
}
}

After the control is instantiated in user.aspx.cs, I would call
functions to add rows as needed:

protected rootnamespace.c ontrols.Default DisplayTable ctrlDisplay;
ctrlDisplay = new rootnamespace.c ontrols.Default DisplayTable();
ctrlDisplay.Add _Table_Row("Use rname", UsernameVariabl e);
ctrlDisplay.Bin d_DataTable();

The problem here is that when I call the Bind_DataTable( ) function, I
get the following error:

Object reference not set to an instance of an object.

Am I trying to compartmentaliz e TOO much here? Have I gone overboard?
Thanks in advance to anyone able to help!

Nov 16 '05 #1
0 1492

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9486
by: Tolga | last post by:
How do insert a user control i have created into a datagrid cell. Basically i am trying to create a column of my user controls. This is what i have tried so far. int i = 0; while (i < dsSignIn1.Tables.Rows.Count) { SignatureControl.SignatureControl sig1 = new SignatureControl.SignatureControl();
4
292
by: Jason | last post by:
I am pretty new to C# so bare with me... The design of my website utilizes tables on every page that look almost identical, save the data they are displaying. I have written the code to create the body of these tables using a DataGrid, so I can dynamically add rows to the table without modifying the HTML. Now, I want to put this DataGrid into a user control that I can include in the HTML code, then control the behavior of that DataGrid...
0
2372
by: psinyc | last post by:
here's a design question or two: 1) let say i have a a hierarcy of user controls for a nav bar. The first one is a general bar (reports, applications, info center, faq, etc), and the second bar (which is also a user control) would be launched on a user click from the first bar. Can I change the calling bar to the called bar (both user controls) in the <td> of that table?
2
1116
by: jm | last post by:
I am still migrating from old ASP to ASP.NET with VB.NET. I created an app as a single file type application and later found out about Visual Studio and code behind class files. Is that the predominant design method? Or just with Visual Studio (guess they are the only ones who can do it)? Does the compiling the .net page do any good? Does it make it more scalable or something? Insights on this issue please. Thank you. ...
4
1416
by: DotNetJunkies User | last post by:
I have created a User Control (a Pareto Chart) using C#. It works great in .Net Windows Apps, but I also need to use it in a Web Application (to view in IE). Everything I read on this seems to be talking about Web Controls (although the terms "Web Control" and "User Control" seem to be used interchangeably). Can I use this Control in IE even though its not a Web Control? Whats the alternative? The Control project is fairly extensive, using...
1
1269
by: Chris Kettenbach | last post by:
Hi I am designing an ASP.net app. Just wanted some opinions on code behind options. My idea is to have a code behind file that the controls will reference and have ascx files for the controls. Then have the aspx pages call the controls. Is this a good approach? I wanted to throw all my business logic in to the code behind file and then on the aspx pages that use the controls just refernce the ascx files. Is this the correct approach?...
1
2126
by: thegame | last post by:
Filling One DataGrid Based on Selection from Another DataGrid - Both in Separate User Controls Hello, I have an interesting dilemma. I have an ASPX page with two user controls (two ASCXs). Both ASCXs have DataGrids on them. The first user control's DataGrid is populated from our database on page load. The DataGrid also has a Select LinkButton.
1
252
by: thewhoracle | last post by:
I have a datagrid, and I have a column that must do the following. -if each row's data member in this column has a certain sequence of characters, it must have a link associated with it that goes to a website that uses the data in this member as part of the address. I'm new at asp.net. I need to know how to conditionally add links when the datagrid gets filled, and i need to know how to get the value of the member in the column...
2
1863
by: tshad | last post by:
User VS 2003, I would like to use User Controls to control the page look with 3 User Controls (...PageTop.ascx, ...NavigateTop.ascx and PageBottom.ascx). I would put the Content User control between the NavigateTop and PageBottom User controls. These files be something like: sdhcPageTop.ascx, ft2PageTop.ascx, sbPageTop.ascx, macPageTop.ascx etc. I would have the same files for the NavigateTop.ascx and PageBottom.asc. The ones that...
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.