Problem with User Control and unset references to controls within

Discussion in 'ASP .Net Web Controls' started by Sam Martin, Mar 30, 2006.

  1. Sam Martin

    Sam Martin Guest

    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

    --
    TIA
    Sam Martin
    Sam Martin, Mar 30, 2006
    #1
    1. Advertising

  2. Sam Martin

    Guest

    Hi,

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

    Regards.
    , Mar 30, 2006
    #2
    1. Advertising

  3. Sam Martin

    Sam Martin Guest

    Re: Problem with User Control and unset references to controls wit

    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>
    --
    TIA
    Sam Martin


    "" wrote:

    > Hi,
    >
    > How are did you load the user control - declaratively or
    > programmatically?
    >
    > Regards.
    >
    >
    Sam Martin, Mar 30, 2006
    #3
  4. Sam Martin

    Guest

    Re: Problem with User Control and unset references to controls wit

    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.
    , Mar 30, 2006
    #4
  5. Sam Martin

    Sam Martin Guest

    Re: Problem with User Control and unset references to controls wit

    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

    --
    TIA
    Sam Martin


    "" 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.
    >
    >
    Sam Martin, Mar 30, 2006
    #5
  6. Sam Martin

    Guest

    Re: Problem with User Control and unset references to controls wit

    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.
    , Mar 30, 2006
    #6
  7. Sam Martin

    Sam Martin Guest

    Re: Problem with User Control and unset references to controls wit

    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

    --
    Sam Martin


    "" 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.
    >
    >
    Sam Martin, Mar 30, 2006
    #7
  8. Sam Martin

    Guest

    Re: Problem with User Control and unset references to controls wit

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

    Regards,
    Andy
    , Mar 30, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    3,237
    Ryan Walberg, MCSD for .NET
    Oct 22, 2004
  2. nyenyec
    Replies:
    0
    Views:
    338
    nyenyec
    Jan 26, 2007
  3. Stefan Ram
    Replies:
    4
    Views:
    925
    Bill Davy
    Dec 7, 2009
  4. Wolfman

    unset TCL_LIBRARY and TK_LIBRARY

    Wolfman, Apr 5, 2010, in forum: Python
    Replies:
    1
    Views:
    857
    Dave Angel
    Apr 5, 2010
  5. Sam Martin

    Problem with User Control and unset references to controls within

    Sam Martin, Mar 30, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    93
    Steve C. Orr [MVP, MCSD]
    Mar 30, 2006
Loading...

Share This Page