473,397 Members | 2,028 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,397 software developers and data experts.

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

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
--
TIA
Sam Martin
Mar 30 '06 #1
4 2125
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Sam Martin" wrote:
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
--
TIA
Sam Martin

Mar 30 '06 #2
sorry, typo on example, in the real case they do, hence the problem

do you have any other ideas?
--
TIA
Sam Martin
"Phillip Williams" wrote:
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Sam Martin" wrote:
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
--
TIA
Sam Martin

Mar 30 '06 #3
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
}
}

--
TIA
Sam Martin
"Sam Martin" wrote:
sorry, typo on example, in the real case they do, hence the problem

do you have any other ideas?
--
TIA
Sam Martin
"Phillip Williams" wrote:
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Sam Martin" wrote:
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
--
TIA
Sam Martin

Mar 30 '06 #4
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; }
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Sam Martin" wrote:
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
}
}

--
TIA
Sam Martin
"Sam Martin" wrote:
sorry, typo on example, in the real case they do, hence the problem

do you have any other ideas?
--
TIA
Sam Martin
"Phillip Williams" wrote:
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Sam Martin" wrote:

> 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
> --
> TIA
> Sam Martin

Mar 30 '06 #5

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

Similar topics

2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
3
by: moondaddy | last post by:
I'm trying to create my first user control and its function is to replace the use of a frames page. I want to position it under the pages header menus and to the right of the pages contents menus....
7
by: moondaddy | last post by:
I have a user control being used instead of a frame page. when the user clicks on a menu item I need to send the ID (integer value) of that menu as a parameter in the postback of the user control...
8
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing...
2
by: Hans Merkl | last post by:
Hi, I am trying to use a user control as EditItemTemplate in a DataList. It loads fine but I can't figure out how to bind to the data of the DataList. Here is what I have got so far: ...
4
by: TS | last post by:
I am creating a User control and i create some dynamic controls in the init handler. one of the controls is a custom validator which i assign a serverValidate event handler. I usally always do my...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...

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.