DropDownList always returning the first item

Discussion in 'ASP .Net' started by Siamak Zahedi, Jan 27, 2005.

  1. Hi,
    weird problem I'm having cant seem to figure out what is going on.

    I have a dropdownlist that gets populated from db and a button that causes
    a post back
    when I assign the value of the dropdownlist to a string var it always
    returns the first item regardless of what was selected

    code is simple

    in the button click event

    string name = ddlName.SelectedItem.ToString();

    I'm learning asp.net and this is my first project so excuse the newbie
    question .
     
    Siamak Zahedi, Jan 27, 2005
    #1
    1. Advertisements

  2. Siamak Zahedi

    Guest Guest

    Did you try “name = ddlName.SelectedItem.Valueâ€.
    ToString() is probably not the selected ListItem value.

    Shaw
     
    Guest, Jan 28, 2005
    #2
    1. Advertisements

  3. yes things ive tryed so far:

    string name = ddlSelectedItem.Value;
    string name = ddlSelectedItem.Text;

    the first one returns the SelectedValue of the first item in the list
    and the Text returns the same as the ToString i had before

    im starting to think its maybe because i have it placed in a panel which is
    also inside a Main panel
    page has a main panel that is visible and a second panel that is not visible
    if the user clicks the post back button on the main panel and all the
    procedures run with out error the second panels is set to visible and page
    is rendered.

    on the second panel i have a databound dropdown (one that is causeing the
    problem) a html File input set to run on server and a Upload button that
    posts back the file selected, the purpose of the drop down is to choose the
    directory where the file is saved in...
     
    Siamak Zahedi, Jan 28, 2005
    #3
  4. Siamak Zahedi

    Karl Seguin Guest

    Siamak:
    Just a guess, but are you rebinding on PostBack? If so you could be
    overwriting the selection made by the user. Make sure to wrap the binding
    code inside of an

    if (!Page.IsPostBack){
    ddlName.DataSource = xxx;
    ddlName.DataBind();
    }

    Karl
     
    Karl Seguin, Jan 28, 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.