why is ddl.selectedValue not retained in viewstate ?

Discussion in 'ASP .Net' started by Guest, Oct 3, 2005.

  1. Guest

    Guest Guest

    I try to move textBoxes and ddl dynamically to a ASP:table and everything
    works correctly, except that for the ddl's the selectedValue is not retained
    (it is for the TextBoxes).
    Here is my code:
    private void LoadTable()
    {
    Table1.Rows.Clear();
    TableRow r = new TableRow();
    TableCell c = new TableCell();
    c.Controls.Add(Page.FindControl("TextBox1"));
    TextBox1.Visible = true;
    c.Controls.Add(Page.FindControl("DropDownList1"));
    DropDownList1.Visible = true;
    if (tbxHidden.Text == "")
    {
    DropDownList1.Items.Clear();
    DropDownList1.Items.Add("Value1");
    DropDownList1.Items.Add("Value2");
    DropDownList1.Items.Add("Value3");
    }
    r.Cells.Add(c);
    Table1.Rows.Add(r);
    TableDummy.Rows.Clear();
    tbxHidden.Text = "IsLoaded";
    TableCell cd= new TableCell();
    TableRow rd = new TableRow();
    cd.Controls.Add(Page.FindControl("tbxHidden"));
    rd.Cells.Add(cd);
    TableDummy.Rows.Add(rd);
    }

    Is this a bug ?
     
    Guest, Oct 3, 2005
    #1
    1. Advertisements

  2. You have to populate the list in the Init event. The viewstate is processed
    between the Init and Load.
     
    Peter Rilling, Oct 3, 2005
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Thanks a lot Peter, this is exactly what I needed.
     
    Guest, Oct 4, 2005
    #3

  4. That's true in part. The part of the lifecycle that is responsible for
    maintaining this is the LoadPostData() method of the IPostBackDataHandler
    interface. Any control that posts form data (textbox, checkbox, ddl, etc.)
    implements IPostBackDataHandler and uses the LoadPostData() method to
    maintain state between postbacks.

    In other words, this has nothing to do with viewstate. Turn off viewstate
    and you will still persist your values via IPostBackDataHandler.

    --
    Jim Cheshire
    JIMCO Software
    http://www.jimcosoftware.com

    FrontPage add-ins for FrontPage 2000 - 2003
     
    JIMCO Software, Oct 4, 2005
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.