Dropdown binding and postbacks - ASP.NET

Posted by DotnetDude on Stack Overflow See other posts from Stack Overflow or by DotnetDude
Published on 2009-03-12T00:54:48Z Indexed on 2010/06/08 23:02 UTC
Read the original article Hit count: 171

Filed under:
|

I have a rather complex page. The ASPX page loads a user control which in turn loads a child User control.

Parent Control

protected override void OnInit(EventArgs e)
{
  //Loads child control
}

In the child user control, I use custom control that inherits from System.Web.UI.HtmlControls.HtmlSelect

ASCX:

<cust:CustDropDownList id="ctlDdl" runat="server"/>

ASCX.CS

protected void Page_Load(object sender, EventArgs e)
{
  //Binds CtlDdl here 
}

When the user clicks on the Save button, the controls get user controls get dynamically reloaded, but Iose the value the user has selected in the dropdown. I run into the chicken and egg problem here.

I think I need to bind the ctlDdl only on if its not a postback, but that results in the dropdown not getting populated.

If I bind it everytime, then i lose user's selection

EDIT: Can someone respond to my comment to Jonathan's answer? Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET