dynamic control and viewState

Discussion in 'ASP .Net Web Controls' started by Frank, Sep 10, 2003.

  1. Frank

    Frank Guest

    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
     
    Frank, Sep 10, 2003
    #1
    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. PABruceFan
    Replies:
    3
    Views:
    4,137
    John Saunders
    Jul 31, 2004
  2. Mick

    Viewstate and dynamic control

    Mick, Jan 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    581
  3. Josema
    Replies:
    2
    Views:
    392
  4. PABruceFan
    Replies:
    3
    Views:
    219
    John Saunders
    Jul 31, 2004
  5. Josema
    Replies:
    0
    Views:
    228
    Josema
    May 9, 2005
Loading...

Share This Page