get value of dynamic control on postback?

Discussion in 'ASP .Net' started by =?Utf-8?B?RGF2ZQ==?=, Aug 3, 2007.

  1. For simplicity sake,

    How do you get the value of selected dropdown on a postback? I understand
    for a dynamic control, you need to re-create it on each postback, but how do
    I modify the code to grab the SelectedValue?

    protected void Page_Load(object sender, EventArgs e)
    {
    DropDownList list = new DropDownList();
    list.ID = "DropDownList1";
    list.Items.Add(new ListItem("1", "One"));
    list.Items.Add(new ListItem("2", "Two"));
    list.Items.Add(new ListItem("3", "Three"));
    PlaceHolder1.Controls.Add(list);
    }
     
    =?Utf-8?B?RGF2ZQ==?=, Aug 3, 2007
    #1
    1. Advertising

  2. Hi,
    So, the controls that were dynamically created are no longer there and
    consequently the values returned from these controls have no place to go.
    They are lost in the viewstate.

    In order to catch these values the dynamically generated controls needs to
    be re-generated at Page_Load. The important thing is to assign the same ID to
    each control. The ViewState uses the ID property of the Control objects to
    reinstate the values.

    If you create a dynamic dropdownlist, conditionally, say in response to some
    other control's click event. Then, on PostBack, you recreate a "new" dropdown
    with the same object name, then "magically" that new dropdown becomes the
    dynamically-created dropdown. You get all of the user-initiated and
    code-generated properties.
    ref:http://www.codeproject.com/aspnet/retainingstate.asp?df=100&forumid=14609&exp=0&select=1495875
    --
    Hope this answers your question.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "Dave" wrote:

    > For simplicity sake,
    >
    > How do you get the value of selected dropdown on a postback? I understand
    > for a dynamic control, you need to re-create it on each postback, but how do
    > I modify the code to grab the SelectedValue?
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > DropDownList list = new DropDownList();
    > list.ID = "DropDownList1";
    > list.Items.Add(new ListItem("1", "One"));
    > list.Items.Add(new ListItem("2", "Two"));
    > list.Items.Add(new ListItem("3", "Three"));
    > PlaceHolder1.Controls.Add(list);
    > }
    >
     
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Aug 3, 2007
    #2
    1. Advertising

  3. Place the code for control creting in the PreInit event. The reason is
    because LoadViewState occurs between Init and Load events.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Dave" <> wrote in message
    news:...
    > For simplicity sake,
    >
    > How do you get the value of selected dropdown on a postback? I understand
    > for a dynamic control, you need to re-create it on each postback, but how

    do
    > I modify the code to grab the SelectedValue?
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > DropDownList list = new DropDownList();
    > list.ID = "DropDownList1";
    > list.Items.Add(new ListItem("1", "One"));
    > list.Items.Add(new ListItem("2", "Two"));
    > list.Items.Add(new ListItem("3", "Three"));
    > PlaceHolder1.Controls.Add(list);
    > }
    >
     
    Eliyahu Goldin, Aug 3, 2007
    #3
    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. Piotr Strycharz
    Replies:
    7
    Views:
    9,564
    Kevin Spencer
    Sep 30, 2003
  2. Larry Bud
    Replies:
    1
    Views:
    356
    bruce barker
    Jan 10, 2007
  3. gnewsgroup
    Replies:
    1
    Views:
    393
    gnewsgroup
    Feb 13, 2008
  4. Nick

    Retrieve PostBack value of dynamic control

    Nick, May 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    164
  5. Replies:
    1
    Views:
    405
    Alessandro Zifiglio
    Apr 20, 2006
Loading...

Share This Page