Dropdownlist selected value lost after postback

Discussion in 'ASP .Net' started by Celine, Sep 2, 2004.

  1. Celine

    Celine Guest

    I have a webform with multiple panels with textboxes, dropdownlists,
    radiobuttonlists, etc. The user navigates through the panels and then
    submits the information at the end.

    The first panel contains a text box where users can enter a search term. A
    "next" button displays the following panel causing a stored procedure to
    populate a dropdown list. The dropdown list displays the results of the
    search, and requires users to select an option.

    The problem is that when the user navigates forward to other panels, the
    selected option in the dropdown list is not retained. The first entry in
    the dropdownlist is what is submitted to the database, not the option the
    user selected. This is because I am populating the list when
    Page.IsPostBack. When I've tried to populate the list when ! IsPostBack is
    true, it doesn't populate correctly. Instead of displaying options based on
    the search term, it displays every item in the database. However, in this
    scenario, the selected value is retained.



    How can I get around this problem?



    Here is the code from the codebehind page



    if (Page.IsPostBack)

    {



    //populate dropOwnerSelect

    SqlCommand cmdSelect;

    SqlDataReader dtrOwner;



    cmdSelect = new SqlCommand( "sp_selectCompanies", conProjData);

    cmdSelect.CommandType = CommandType.StoredProcedure;

    cmdSelect.Parameters.Add("@name", txtOwner.Text);

    conProjData.Open();

    dtrOwner = cmdSelect.ExecuteReader();



    dropOwnerSelect.DataSource = dtrOwner;

    dropOwnerSelect.DataValueField = "CompanyID";

    dropOwnerSelect.DataTextField = "CompanyName";

    dropOwnerSelect.DataBind();



    dtrOwner.Close();

    conProjData.Close();

    }
    Celine, Sep 2, 2004
    #1
    1. Advertising

  2. Celine

    James Thomas Guest

    > The problem is that when the user navigates forward to other panels,
    > the selected option in the dropdown list is not retained. The first
    > entry in the dropdownlist is what is submitted to the database, not
    > the option the user selected. This is because I am populating the
    > list when Page.IsPostBack. When I've tried to populate the list when
    > ! IsPostBack is true, it doesn't populate correctly. Instead of
    > displaying options based on the search term, it displays every item
    > in the database. However, in this scenario, the selected value is
    > retained.


    You could store the value of the ListItem in question in the Session
    state and then when after the form loads, you can do:

    dropDown.SelectedValue = Session["dropDownValue"]

    James
    James Thomas, Sep 3, 2004
    #2
    1. Advertising

  3. But then session state changes after the form posts back again and the
    selected item is not retained.

    Is there another way of approaching this so that the value can be kept
    throughout the postbacks to the form?

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Celine McLean, Sep 3, 2004
    #3
  4. Celine

    James Thomas Guest

    Celine McLean wrote:

    > But then session state changes after the form posts back again and the
    > selected item is not retained.
    >
    > Is there another way of approaching this so that the value can be kept
    > throughout the postbacks to the form?
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!


    Why change it on postback? I do something similar and I only change it
    when a new item is selected from the drop down (or I in-code change it,
    I just call my SelectionChanged event with null, null as arguments and
    that method changes the Session variable, not the Page_Load method).

    James
    James Thomas, Sep 3, 2004
    #4
    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. Iain
    Replies:
    3
    Views:
    927
  2. Nathan Sokalski
    Replies:
    0
    Views:
    604
    Nathan Sokalski
    Oct 5, 2008
  3. MU
    Replies:
    3
    Views:
    6,491
    Lloyd Sheen
    Oct 24, 2008
  4. BobbyMy
    Replies:
    0
    Views:
    191
    BobbyMy
    Aug 27, 2005
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    289
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page