473,387 Members | 1,606 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,387 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 1463

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.