User Control Disappears After PostBack - Why?

G

Guadala Harry

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
 
K

Kevin Spencer

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.
 
J

John Saunders

Guadala Harry said:
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
}
}
 
G

Guadala Harry

Thanks John and Kevin

-GH



John Saunders said:
EnableViewState="true".

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
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top