473,569 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="DataListMai n" runat="server" RepeatDirection ="Horizontal "
RepeatColumns=" 4"
Width="100%" GridLines="Vert ical">
<ItemTemplate >
asdf
</ItemTemplate>
</asp:DataList>

VS.NET 2003 automatically puts in the code behind:

protected System.Web.UI.W ebControls.Data List 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 NullReferenceEx ception is obviously thrown when trying access the reference.

eg.
private void Page_Load(objec t sender, System.EventArg s 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 2143
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="DataListMai n" runat="server" RepeatDirection ="Horizontal "
RepeatColumns=" 4"
Width="100%" GridLines="Vert ical">
<ItemTemplate >
asdf
</ItemTemplate>
</asp:DataList>

VS.NET 2003 automatically puts in the code behind:

protected System.Web.UI.W ebControls.Data List 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 NullReferenceEx ception is obviously thrown when trying access the reference.

eg.
private void Page_Load(objec t sender, System.EventArg s 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="DataListMai n" runat="server" RepeatDirection ="Horizontal "
RepeatColumns=" 4"
Width="100%" GridLines="Vert ical">
<ItemTemplate >
asdf
</ItemTemplate>
</asp:DataList>

VS.NET 2003 automatically puts in the code behind:

protected System.Web.UI.W ebControls.Data List 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 NullReferenceEx ception is obviously thrown when trying access the reference.

eg.
private void Page_Load(objec t sender, System.EventArg s 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.as cx

<%@ Control Language="c#" AutoEventWireup ="false"
Codebehind="Ite mDataList.ascx. cs" Inherits="MyNam espace.ItemData List"
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5"%>
<asp:DataList id="DataListMai n" runat="server" RepeatDirection ="Horizontal "
RepeatColumns=" 4"
Width="100%" GridLines="Vert ical">
<ItemTemplate >
Template item
</ItemTemplate>
</asp:DataList>

CODEBEHIND
ItemDataList.as cx.cs

namespace MyNamespace
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

/// <summary>
/// Summary description for ItemDataList.
/// </summary>
public class ItemDataList : System.Web.UI.U serControl
{
public System.Web.UI.W ebControls.Data List 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(objec t sender, System.EventArg s e)
{
this.DataListMa in.DataSource = ds; // this.DataListMa in is undefined
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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="DataListMai n" runat="server" RepeatDirection ="Horizontal "
RepeatColumns=" 4"
Width="100%" GridLines="Vert ical">
<ItemTemplate >
asdf
</ItemTemplate>
</asp:DataList>

VS.NET 2003 automatically puts in the code behind:

protected System.Web.UI.W ebControls.Data List 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 NullReferenceEx ception is obviously thrown when trying access the reference.

eg.
private void Page_Load(objec t sender, System.EventArg s 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.DataListMa in.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.as cx

<%@ Control Language="c#" AutoEventWireup ="false"
Codebehind="Ite mDataList.ascx. cs" Inherits="MyNam espace.ItemData List"
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5"%>
<asp:DataList id="DataListMai n" runat="server" RepeatDirection ="Horizontal "
RepeatColumns=" 4"
Width="100%" GridLines="Vert ical">
<ItemTemplate >
Template item
</ItemTemplate>
</asp:DataList>

CODEBEHIND
ItemDataList.as cx.cs

namespace MyNamespace
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

/// <summary>
/// Summary description for ItemDataList.
/// </summary>
public class ItemDataList : System.Web.UI.U serControl
{
public System.Web.UI.W ebControls.Data List 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(objec t sender, System.EventArg s e)
{
this.DataListMa in.DataSource = ds; // this.DataListMa in is undefined
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(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="DataListMai n" runat="server" RepeatDirection ="Horizontal "
> RepeatColumns=" 4"
> Width="100%" GridLines="Vert ical">
> <ItemTemplate >
> asdf
> </ItemTemplate>
> </asp:DataList>
>
> VS.NET 2003 automatically puts in the code behind:
>
> protected System.Web.UI.W ebControls.Data List 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 NullReferenceEx ception is obviously thrown when trying access the reference.
>
> eg.
> private void Page_Load(objec t sender, System.EventArg s 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
3015
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 or more user controls dynamically by adding to a placeholder defined in page_load. I've included the sample code for how we are accessing one. The...
6
11274
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" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
3
6431
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. How can I set the top and left position of this control? -- moondaddy@nospam.com
7
7725
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 which will be used to query sql server to repopulate the datagrid in the user control. I also wrapped the user control in a panel element so I...
8
2256
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 happens. dim filt as string ... build filter string... UserControl.ReportFilter = filt
2
3427
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: //Page_load of my user control protected void Page_Load(object sender, EventArgs e) { IDataItemContainer DataContainer = this.Parent as
4
4180
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 controls as custom server controls and don't understand why this event won't fire. I figured if the creation of the control was in the init, it...
9
3173
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 load with different data (locations, departments, etc.).
4
2472
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 unsuccessfully in creating the whole pane as a user control and have succeeded in adding the pane and then dynamically adding the content which is a user...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.