473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 DefaultDisplayTable.ascx that will be included in the
HTML of the user.aspx page:

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

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

<asp:datagrid id="grdUser" runat="server" datasource="<%# loTable%>"
autogeneratecolumns="False">
<columns>
<asp:boundcolumn datafield="colLabel"> </asp:boundcolumn>
<asp:boundcolumn datafield="colData"> </asp:boundcolumn>
</columns>
</asp:datagrid>

Then in the code-behind of this control, DefaultDisplayTable.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 DefaultDisplayTable : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid grdUser;
protected DataTable loTable;
public DefaultDisplayTable()
{
loTable = new DataTable("DataTable");

DataColumn dcLabel = new DataColumn();
dcLabel.DataType = System.Type.GetType("System.String");
dcLabel.ColumnName = "colLabel";
loTable.Columns.Add(dcLabel);

DataColumn dcData = new DataColumn();
dcData.DataType = System.Type.GetType("System.String");
dcData.ColumnName = "colData";
loTable.Columns.Add(dcData);
}
public void Add_Table_Row(string lsLabelText, string lsDataText)
{
DataRow drLabel;
drLabel = loTable.NewRow();
drLabel["colLabel"] = lsLabelText;
loTable.Rows.Add(drLabel);

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

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

protected rootnamespace.controls.DefaultDisplayTable ctrlDisplay;
ctrlDisplay = new rootnamespace.controls.DefaultDisplayTable();
ctrlDisplay.Add_Table_Row("Username", UsernameVariable);
ctrlDisplay.Bind_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 compartmentalize TOO much here? Have I gone overboard?
Thanks in advance to anyone able to help!

Nov 16 '05 #1
0 1466

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

Similar topics

2
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 <...
4
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...
0
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...
2
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...
4
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...
1
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. ...
1
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). ...
1
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.