dropdown list in custom control not persiting databind

J

Joel Barsotti

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>
 
T

Teemu Keiski

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();
}
}
}
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top