NullReference to Web Control in UserControl... please help!

G

Guest

Hi,

I have got a User Control that contains for the sake of argument, a single
DataList control.

eg.
<asp:DataList id="DataListMain" runat="server" RepeatDirection="Horizontal"
RepeatColumns="4"
Width="100%" GridLines="Vertical">
<ItemTemplate>
asdf
</ItemTemplate>
</asp:DataList>

VS.NET 2003 automatically puts in the code behind:

protected System.Web.UI.WebControls.DataList DataList1;

in the Page_Load event of the control, this.DataList1 is always undefined.

I've tried this again with other controls, created new really simple test user
control to see if the same thing happens, and it does. I can't seem to be
able to get a reference to the Web Control held within my UserControl.

A NullReferenceException is obviously thrown when trying access the reference.

eg.
private void Page_Load(object sender, System.EventArgs e)
{
this.DataList1.DataSource = <some datasource>; // this is where
DataList1 == <undefined>
}

I must be missing something yeah?

Please help
 
G

Guest

If the syntax of the code were as you posted then the mistake is that you
declared the protected instance of the datalist with a name (datalist1)
different than the id (DataListMain) you gave to the datalist on the HTML
markup. The names have to match.
 
G

Guest

sorry, typo on example, in the real case they do, hence the problem

do you have any other ideas?
 
G

Guest

heres a full listing

ASCX
ItemDataList.ascx

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="ItemDataList.ascx.cs" Inherits="MyNamespace.ItemDataList"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:DataList id="DataListMain" runat="server" RepeatDirection="Horizontal"
RepeatColumns="4"
Width="100%" GridLines="Vertical">
<ItemTemplate>
Template item
</ItemTemplate>
</asp:DataList>

CODEBEHIND
ItemDataList.ascx.cs

namespace MyNamespace
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for ItemDataList.
/// </summary>
public class ItemDataList : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.DataList DataListMain;

private object ds;

/// <summary>
/// Gets or sets the data source.
/// </summary>
/// <value>The data source.</value>
public object DataSource
{
set { ds = value; }
get { return this.ds; }
}

private void Page_Load(object sender, System.EventArgs e)
{
this.DataListMain.DataSource = ds; // this.DataListMain is undefined
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
G

Guest

I think that the "undefined value" error that you got is referring to ds
instead of the DAtaListMain object. When the load event fires, you have not
set the DataSource property of the control. To verify that enclose the line
that you have in Page_load within an if statement that tests ds is not null.
If that turns out to be the case then you are better off setting the
datasource property of the datalist within the set segment of the property
DataSource of the control, e.g.

public object DataSource
{
set { ds = value;
if (value !=null) this.DataListMain.DataSource = ds;
}
get { return this.ds; }
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top