By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

Question about user controls and DataGrids

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.