dynamic control and viewState

F

Frank

Hi all,

I want to create a control dynamically and keep viewState.
I got the following code from a google search which works.

private void Page_Load(object sender, System.EventArgs e) {
TextBox tb = new TextBox();
tb.ID = "vic";
FindControl("Form1").Controls.Add( tb );
if(!IsPostBack)
tb.Style.Add("background-color","red");
}

but if I move FindControl("Form1").Controls.Add( tb ); to the end like this:
TextBox tb = new TextBox();
tb.ID = "vic";
if(!IsPostBack)
tb.Style.Add("background-color","red");
FindControl("Form1").Controls.Add( tb );

It does not work anymore. ViewState is not written to client. Does it mean
that the viewState is
added by .NET at the line of tb.Style.Add("background-color","red") ?

Thanks.
Frank
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top