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