Retrieving dynamically added items on postback

Discussion in 'ASP .Net' started by echan8@gmail.com, May 30, 2006.

  1. Guest

    I have a dropdown list in a datagrid which I am dynamically adding
    items to with JavaScript.

    When I try to access the value of the dropdown list on postback, there
    is no value. The value is null. I get the feeling I'm supposed to do
    something else, but I'm not sure what.

    If anyone can help, I would appriciate it. Thanks.

    foreach( DataGridItem di in DataGrid1.Items )
    {

    DropDownList ddl = (DropDownList) di.FindControl("ddlItems");
    Label lblID = (Label)di.FindControl("id");

    int id = Convert.ToInt32(lblID.Text);
    int itemvalue = Convert.ToInt32(ddl.SelectedValue); // the
    item value here is null

    // Do stuff with items here.
    }
    , May 30, 2006
    #1
    1. Advertising

  2. the browser only posts back the selected value of a drop down, so the server
    has no way of knowing the changes the client code made. the postback data
    support in the dropdown, expects the postback value to be in its list of
    value, if not, it ignores it.

    you shoudl decide hoiw the client code is going to pass the additional value
    to the browser. if you only need the selected value, then no more client
    code is required. then in the server code, in OnInit (before postback data
    is processed), you need to add the selects postback value (look in the forms
    collection) as a valid value in the dropdown.

    -- bruce (sqlwork.com)


    <> wrote in message
    news:...
    >I have a dropdown list in a datagrid which I am dynamically adding
    > items to with JavaScript.
    >
    > When I try to access the value of the dropdown list on postback, there
    > is no value. The value is null. I get the feeling I'm supposed to do
    > something else, but I'm not sure what.
    >
    > If anyone can help, I would appriciate it. Thanks.
    >
    > foreach( DataGridItem di in DataGrid1.Items )
    > {
    >
    > DropDownList ddl = (DropDownList) di.FindControl("ddlItems");
    > Label lblID = (Label)di.FindControl("id");
    >
    > int id = Convert.ToInt32(lblID.Text);
    > int itemvalue = Convert.ToInt32(ddl.SelectedValue); // the
    > item value here is null
    >
    > // Do stuff with items here.
    > }
    >
    bruce barker \(sqlwork.com\), May 30, 2006
    #2
    1. Advertising

  3. my$.02 Guest

    You might try the Request.Form[controlName] property to get the selected
    value from the drop down list, though you will need to use the control name
    passed to the client. Since you are embedding the drop down list in a
    datagrid, it is going to be some combination of the grid id and the drop down
    list id. I cannot remember off the top of my head, but you can view the HTML
    source to see the pattern.
    my$.02, May 30, 2006
    #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. Dotnet Gruven
    Replies:
    3
    Views:
    1,730
    Steven Cheng[MSFT]
    Jan 23, 2006
  2. Replies:
    0
    Views:
    1,667
  3. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    1,233
    Scott Roberts
    Nov 20, 2007
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    458
    Misbah Arefin
    Feb 14, 2008
  5. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    941
    Beatrix
    Nov 20, 2007
Loading...

Share This Page