User Control Disappears After PostBack - Why?

Discussion in 'ASP .Net' started by Guadala Harry, Jul 1, 2004.

  1. 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
    Guadala Harry, Jul 1, 2004
    #1
    1. Advertising

  2. If you dynamically add a Control to the page during a single Request, be
    aware that every PostBack is a new Request. ViewState has nothing to do with
    it. You need to add the Control with each PostBack. HTTP is stateless.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Guadala Harry" <> wrote in message
    news:...
    > 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
    >
    >
    Kevin Spencer, Jul 1, 2004
    #2
    1. Advertising

  3. "Guadala Harry" <> wrote in message
    news:...
    > 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
    John Saunders, Jul 1, 2004
    #3
  4. Thanks John and Kevin

    -GH



    "John Saunders" <> wrote in message
    news:...
    > "Guadala Harry" <> wrote in message
    > news:...
    > > 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
    >
    >
    Guadala Harry, Jul 2, 2004
    #4
    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. Ryu
    Replies:
    1
    Views:
    3,219
    Lucas Tam
    Aug 18, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,803
    Smokey Grindel
    Dec 2, 2006
  3. Sami Rehman
    Replies:
    1
    Views:
    1,276
    Mark Rae [MVP]
    Sep 3, 2007
  4. Svetoslav Vasilev

    Static user control disappears after postback

    Svetoslav Vasilev, Nov 19, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    955
    Svetoslav Vasilev
    Nov 19, 2007
  5. Hagai Amiel via .NET 247

    Data Grid Disappears after postback

    Hagai Amiel via .NET 247, Oct 17, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    154
    Scott Mitchell [MVP]
    Oct 18, 2004
Loading...

Share This Page