Question about user controls and DataGrids

J

Jason

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!
 
?

=?ISO-8859-1?Q?Anders_Nor=E5s?=

Jason said:
After the control is instantiated in user.aspx.cs, I would call
functions to add rows as needed:
(abridged)
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.

You do this in the page containing the user control, right? If so you
probably want call this method on that instance in the page, and not an
instance of the code behind class that you've created in code.
To find a user control within the page use the FindControl method of the
page. The method searches the naming container for a control with the
specified id. The method returns the base type Control, so you'll have
to cast the result to your DefaultDisplayTable type.

Below is an example of how you can change your code:
DefaultDisplayTable ctrlDisplay=Page.FindControl("tblDisplay") as
DefaultDisplayTable;
ctrlDisplay.Add_Table_Row("Username", UsernameVariable);
ctrlDisplay.Bind_DataTable();

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
C

Cor Ligthert

Jason,

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

I think yes, it is a complete explanation however my expirience is that
those seldom are readed.

Try to do it piece by piece in a new message and than not all your questions
in once because those are often as well ignored or get a very flaw answer.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top