Drop down list selected value not submitting

Discussion in 'ASP .Net' started by nz mark in aus, Feb 14, 2006.

  1. Hi there

    I am populating an empty <asp:dropdownlist on the client side using
    Javascript using the following code

    Heres my dropdown list
    <asp:dropdownlist id="ddlDealers" Runat="server"></asp:dropdownlist>

    heres the Java
    var myDropDownList = frmMain.ddlDealers;

    for (var cnt=0;cnt<=dealers.length-1;cnt = cnt + 2){
    var opt = new Option(dealers[cnt],dealers[cnt],0,0);
    myDropDownList.options[myDropDownList.options.length] = opt;
    }
    where dealers() is an array of values

    the list is populated correctly and it all looks fine
    my problem is that when I post the page using a <:asp Button the
    selected value of the list is not being picked up on the server. Lets
    say I selected 'Steve' in my drop down list, a simple test in my
    Page_onload as follows returns NULL.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Response.Write myDropDownList.SelectedValue
    End Sub

    The only time I can get the selected value from a dropdown is when I
    have populated the drop down at the server in the normal .NET fashion,
    but this requires an extra trip to the server which I am trying to
    avoid.

    Any ideas??

    Mark
     
    nz mark in aus, Feb 14, 2006
    #1
    1. Advertising

  2. Have you tried using

    Request.Form("myDropDownList")

    I have had issues with postbacks and grabbing .SelectedValue from a DD.
    Request.Form always seems to help in those weird situations.

    Hope it helps,
    Jeremy Reid
    http://blackstaronline.net
     
    blackstaronline.net, Feb 15, 2006
    #2
    1. Advertising

  3. Hey that works thanks.

    I guess because the Drop down list items aren't created on the server
    side the client side selected item does not match with the server side
    data, or something like that. Meaning you have to revert to a more
    classic ASP style Request.Form

    Thanks
    Mark
     
    nz mark in aus, Feb 15, 2006
    #3
  4. nz mark in aus

    Darren Kopp Guest

    It's because with dynamically added options, such as that, are not
    persisted in ViewState, so the DDL control doesn't actually know
    anything about those members.

    When you post back, it parses the view state to look for the value you
    are looking for. Request.Form gets the post variables, rather than
    matching the posted selected value to the value in view state.
     
    Darren Kopp, Feb 15, 2006
    #4
  5. thanks for the info

    So if I want to dynamically populate drop down lists, is using
    Request.Form the best method to use or is there a more elegant
    solution. I am using Callback to retrieve the list data from the
    server

    var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
    etc..

    can the viewstate be updated during this callback to the server?
     
    nz mark in aus, Feb 16, 2006
    #5
    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. Stephen Adam
    Replies:
    3
    Views:
    12,125
    Stephen Adam
    Nov 30, 2004
  2. weiwei
    Replies:
    0
    Views:
    1,035
    weiwei
    Jan 5, 2007
  3. jaysch
    Replies:
    0
    Views:
    1,245
    jaysch
    Apr 30, 2011
  4. Replies:
    5
    Views:
    284
  5. Replies:
    0
    Views:
    179
Loading...

Share This Page