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