Problem with User Control and unset references to controls within

S

Sam Martin

Hi,

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

eg.
<asp:DataList id="DataList1" 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
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 the UserControl.

I must be missing something yeah?

Please help
 
C

CaffieneRush

Hi,

How are did you load the user control - declaratively or
programmatically?

Regards.
 
S

Sam Martin

Thx for reply,

i should have said, declaratively

e.g.

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<Nc:ItemDataList ID="MyItemDataLisy" runat="server"></Nc:ItemDataList>

</form>
</body>
 
C

CaffieneRush

How did you find out that DataList1 is undefined?
Did you set a breakpoint in your user control's Page_Load and examined
DataList?

Regards.
 
S

Sam Martin

yes, exactly that. same applied for other controls i declared in the ascx
file.

here's a proper listing to help, on the page_load, where i'm setting the
datasource of the DataList is where the exception is thrown.

Although the DataListMain variable is declared, and the <asp:DataList> in
the ascx file, the refernece is always undefined. Is there anything i'm
missing from the delcaration in the host Page (aspx) file?

ASCX File

<%@ 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>

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)
{
// EXCEPTION THROWN HERE as
// this.DataListMain is UNDEFINED!!
this.DataListMain.DataSource = ds;
}

#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
}
}

it's such a simple thing i'm trying to, i don't know what's wrong though.

Thanks for your reply
 
C

CaffieneRush

One last thing, the datalist as written will not display on the client
browser if it is not bound to a data source.
However, if you put a breakpoint in the user control's Page_Load
handler, you should still see that the datalist is defined.
 
S

Sam Martin

yes, you're right. i noticed just after i posted that i'd left out the
DataBind call.

thanks for your help, it's often by have someone ask you questions about it,
that you spot mistakes/ try other ways.

i've just tried adding the control again, this time by dragging it from the
solution explorer and not manually declaring it. not sure what difference
this would have made, but the reference to the object is now there!!

<%@ Register TagPrefix="uc1" TagName="ItemDataList" Src="ItemDataList.ascx" %>

^^ is the Register tag VS put in, only noticable difference is that the
TagPrefix is different and the MyNamespace isn't in the Src. I dunno, it's
working now anyway

Thanks for your help
 
C

CaffieneRush

Glad to be of help. (Instead of studying for the 229 exam, I'm
procrastinating by helping in a newsgroup :)

Regards,
Andy
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top