Re: User Control Disappears After PostBack - Why?

From: John Saunders (johnwsaundersiii_at_notcoldmail.com)
Date: 07/01/04


Date: Thu, 1 Jul 2004 18:09:51 -0400


"Guadala Harry" <gman@hman.com> wrote in message
news:OCsNV06XEHA.3664@TK2MSFTNGP12.phx.gbl...
> I have a simple user control (see below) that has EnableViewState="true".
I
> place it on an ASPX page at runtime using a PlaceHolder - which also has
> EnableViewState="true".
>
> After a postback, the aspx page renders back to the client, but the user
> control is not on the rendered page. Why? I thought ViewState was supposed
> to keep things intact between postbacks. FWIW: during the postback the
code
> does nothing with respect to the user control or to the PlaceHolder within
> which it resides. Additonally, the hosting aspx page has a few WebControls
> (TextBox, DropDownLists, etc..) and they retain their respective values
> between postbacks as expected. What's the deal with the user control?
>
> <%@ Control EnableViewState="true" %>
> <table>
> <tr>
> <td>
> This text is in a table in the user control
> </td>
> </tr>
> </table>
>
> Thanks!
>
> -GH

Didn't you get an answer to this already?

Oh, well, in case I'm thinking of some other question, the answer is that
you have to load dynamic controls on ALL page requests, not just the initial
request. You also need to load them in the same order each time if any of
them use ViewState. So, for instance:

private void Page_Load(object sender, EventArgs e)
{
    // Dynamically load the control, then:
    if (!Page.IsPostBack)
    {
        // Do dynamic initialization of controls which need such
initialization only once
    }
}

-- 
John Saunders
johnwsaundersiii at hotmail


Relevant Pages

  • Re: Are dynamically loaded controls preserved over a postback?
    ... you have to recreate the dynamic comtrol on postback. ... > I have a placeholder, which gets a user control added in when the page ... This user control contains a dropdownlist, ...
    (microsoft.public.dotnet.framework.aspnet)
  • User Control Disappears After PostBack - Why?
    ... I have a simple user control that has EnableViewState="true". ... After a postback, the aspx page renders back to the client, but the user ... does nothing with respect to the user control or to the PlaceHolder within ...
    (microsoft.public.dotnet.framework.aspnet)
  • Are dynamically loaded controls preserved over a postback?
    ... I have a placeholder, which gets a user control added in when the page first loads. ... This user control contains a dropdownlist, and I would like to get hold of the value of this drop down list form the calling page on postback. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Are dynamically loaded controls preserved over a postback?
    ... If the value of that dropdownlist isn't preserved (ie if recreating the user control resets the child controls), ... I have a placeholder, which gets a user control added in when the page ... over the postback. ... Alan Silver ...
    (microsoft.public.dotnet.framework.aspnet)
  • User control - How to set properties?
    ... I created a user control and included it in a aspx page. ... I dont ... And in the containing aspx page, I have a placeholder for this user control. ...
    (microsoft.public.dotnet.general)