tags:

views:

159

answers:

2

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

A: 

With custom controls, you have to manage the state. There is a sort of bubble-up effect where the state is passed along. If you don't deal with it, you don't get state.

This link will get you started: Server Control Custom State Management

Look for

    Protected Overrides Sub LoadViewState( _
        ByVal savedState As Object)
        Dim p As Pair = TryCast(savedState, Pair)
        If p IsNot Nothing Then
            MyBase.LoadViewState(p.First)
            CType(Author, IStateManager).LoadViewState(p.Second)
            Return
        End If
        MyBase.LoadViewState(savedState)
    End Sub

at Custom Property State Management Example

Greg Ogle
A: 

Just override the OnInit method in the user control and load the custom control from that method. ASP.NET sets viewstate tracking after OnInit and before PageLoad. Therefore the defaults that are being set when you load the control are being treated as changes by the viewstate engine and are being reset on the postback.

See this article on viewstate for a detailed explanation:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/truly-understanding-viewstate.aspx

Jonathan Parker
I am confused. Does ASP.NET manage viewstate from controls that are inherited from System.Web.UI.HtmlControls?
DotnetDude
I'm assuming the custom control uses viewstate (the dropdown list definitely would) in which case yes, ASP.NET will manage the viewstate when it is added to the pages collection.
Jonathan Parker
I asked a question about this earlierhttp://stackoverflow.com/questions/606887/which-controls-have-viewstate-maintainedand one of the user said that the .NET framework manages viewstate for only ASP.NET controls. Do you know if this is correct?
DotnetDude
If a user control uses the ViewState property then it will be managed by ASP.NET. This means that to use viewstate all you need to do is store your data in the viewstate dictionary and retrieve it when needed. ASP.NET won't automatically store values for your control though.
Jonathan Parker
@JP - When you say "ViewState property", do you mean using the ViewState object to store values?(Ex: ViewState["test"] = "test"?
DotnetDude
Yep. When you set ViewState["test"] = "test" before Page_Load it will be the default but if you call ViewState["test"] = "test" during/after page load then ASP.NET will have already set the ViewState to track changes and thus the value you set will be treated as dirty.
Jonathan Parker