|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem with User Control and unset references to controls withinI 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 -- TIA Sam Martin Hi,
How are did you load the user control - declaratively or programmatically? Regards. 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> -- Show quoteHide quoteTIA Sam Martin "CaffieneR***@gmail.com" wrote: > Hi, > > How are did you load the user control - declaratively or > programmatically? > > Regards. > > 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. 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 -- Show quoteHide quoteTIA Sam Martin "CaffieneR***@gmail.com" wrote: > 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. > > 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. 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 -- Show quoteHide quoteSam Martin "CaffieneR***@gmail.com" wrote: > 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. > > Glad to be of help. (Instead of studying for the 229 exam, I'm
procrastinating by helping in a newsgroup :) Regards, Andy
row count in inherited GridView control
Use windows control in asp.net 2.0 GridView - can't reference value of a non-visible cell?? Dynamically created (and selected) radiobuttons fire CheckedChanged event on Postback error in adding user controls at runtime Re: How to get underlying data of TreeView control? (ASP.NET 2.0) want to create custom control using graphics Design Time Error: Error Rendering Control ValidateEvent bug with table and radio button Panel with Header |
|||||||||||||||||||||||