dropdown list in custom control not persiting databind

Discussion in 'ASP .Net Building Controls' started by Joel Barsotti, Jan 13, 2004.

  1. testMy Custom Control doesn't has a drop down that pulls it's information
    from another class that I use to get generic
    here's the anotated version of what I'm doing:

    public class AddressControl : System.Web.UI.WebControls.WebControl,
    System.Web.UI.INamingContainer
    {
    protected System.Web.UI.WebControls.DropDownList state = new
    DropDownList();

    protected override void OnLoad(EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    state.DataSource = MailingFunctions.GetStatesList();
    state.DataValueField = "abr";
    state.DataTextField = "name";
    state.EnableViewState = true;
    state.DataBind();
    }
    }

    protected override void CreateChildControls()
    {
    Controls.Add(state);
    }
    }

    implemented on page as such:

    <%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
    Inherits="StockLayouts.test1" %>

    <%@ Register TagPrefix="cc1" Namespace="StockLayouts.CustomControls"
    Assembly="StockLayoutsClassLib" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

    <html>

    <head>

    <title>test</title>

    <meta content="Microsoft Visual Studio 7.0" name=GENERATOR>

    <meta content=C# name=CODE_LANGUAGE>

    <meta content=JavaScript name=vs_defaultClientScript>

    <meta content=http://schemas.microsoft.com/intellisense/ie5
    name=vs_targetSchema>

    </head>

    <body MS_POSITIONING="FlowLayout">

    <form id=test method=post runat="server"><asp:label id=myLabel
    runat="server"></asp:label>

    <hr width="100%">

    <cc1:addresscontrol id=testAddress runat="server"></cc1:addresscontrol><br>

    <asp:button id="myButton" text="Submit Info"
    runat="server"></asp:button></form><br >

    <a href="/test.aspx">click here</a>


    </body>

    </html>
    Joel Barsotti, Jan 13, 2004
    #1
    1. Advertising

  2. Joel Barsotti

    Teemu Keiski Guest

    Hi, CreateChildControls will be called only at PreRender on initial request
    (after Load), and at postback before Load. Sure thing is to specify your
    control as follows:

    public class AddressControl : System.Web.UI.WebControls.WebControl,
    System.Web.UI.INamingContainer
    {

    protected override void CreateChildControls()
    {
    Controls.Clear();
    DropDownList state=new DropDownList();
    Controls.Add(state);
    if (!Page.IsPostBack)
    {
    state.DataSource = MailingFunctions.GetStatesList();
    state.DataValueField = "abr";
    state.DataTextField = "name";
    state.DataBind();
    }
    }
    }

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    "Joel Barsotti" <> wrote in message
    news:%...
    > testMy Custom Control doesn't has a drop down that pulls it's information
    > from another class that I use to get generic
    > here's the anotated version of what I'm doing:
    >
    > public class AddressControl : System.Web.UI.WebControls.WebControl,
    > System.Web.UI.INamingContainer
    > {
    > protected System.Web.UI.WebControls.DropDownList state = new
    > DropDownList();
    >
    > protected override void OnLoad(EventArgs e)
    > {
    > if (!Page.IsPostBack)
    > {
    > state.DataSource = MailingFunctions.GetStatesList();
    > state.DataValueField = "abr";
    > state.DataTextField = "name";
    > state.EnableViewState = true;
    > state.DataBind();
    > }
    > }
    >
    > protected override void CreateChildControls()
    > {
    > Controls.Add(state);
    > }
    > }
    >
    > implemented on page as such:
    >
    > <%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
    > Inherits="StockLayouts.test1" %>
    >
    > <%@ Register TagPrefix="cc1" Namespace="StockLayouts.CustomControls"
    > Assembly="StockLayoutsClassLib" %>
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    >
    > <html>
    >
    > <head>
    >
    > <title>test</title>
    >
    > <meta content="Microsoft Visual Studio 7.0" name=GENERATOR>
    >
    > <meta content=C# name=CODE_LANGUAGE>
    >
    > <meta content=JavaScript name=vs_defaultClientScript>
    >
    > <meta content=http://schemas.microsoft.com/intellisense/ie5
    > name=vs_targetSchema>
    >
    > </head>
    >
    > <body MS_POSITIONING="FlowLayout">
    >
    > <form id=test method=post runat="server"><asp:label id=myLabel
    > runat="server"></asp:label>
    >
    > <hr width="100%">
    >
    > <cc1:addresscontrol id=testAddress
    > runat="server"></cc1:addresscontrol><br>
    >
    > <asp:button id="myButton" text="Submit Info"
    > runat="server"></asp:button></form><br >
    >
    > <a href="/test.aspx">click here</a>
    >
    >
    > </body>
    >
    > </html>
    >
    >
    Teemu Keiski, Jan 16, 2004
    #2
    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. Steven

    DropDown List Custom Control

    Steven, Apr 26, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    6,341
    Steven
    Apr 26, 2005
  2. slfarley
    Replies:
    0
    Views:
    393
    slfarley
    Aug 14, 2007
  3. Matt Sokol
    Replies:
    2
    Views:
    365
    Theo Tillotson
    Aug 7, 2003
  4. Vishal Patel via .NET 247

    persiting data in asp.net

    Vishal Patel via .NET 247, Mar 19, 2005, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    121
    Brock Allen
    Mar 21, 2005
  5. Michael
    Replies:
    0
    Views:
    213
    Michael
    Dec 29, 2003
Loading...

Share This Page