Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > User Control Disappears After PostBack - Why?

Reply
Thread Tools

User Control Disappears After PostBack - Why?

 
 
Guadala Harry
Guest
Posts: n/a
 
      07-01-2004
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


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      07-01-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      07-01-2004
"Guadala Harry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Guadala Harry
Guest
Posts: n/a
 
      07-01-2004
Thanks John and Kevin

-GH



"John Saunders" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Guadala Harry" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static user control disappears after postback Svetoslav Vasilev ASP .Net Building Controls 0 11-19-2007 09:14 AM
Dynamically created control disappears after postback. Sami Rehman ASP .Net 1 09-03-2007 10:51 AM
Data Grid Disappears after postback Hagai Amiel via .NET 247 ASP .Net Datagrid Control 1 11-14-2004 07:14 AM
Data Grid Disappears after postback Hagai Amiel via .NET 247 ASP .Net Datagrid Control 2 10-18-2004 05:39 PM
Controls added dynamically disappears after postback Ryu ASP .Net 1 08-18-2004 05:17 AM



Advertisments