Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here's the aspx:

ASP.NET
<asp:DropDownList ID="LocationSearch" Width="99%" AutoPostBack="true" OnSelectedIndexChanged="SearchTerms_Changed" runat="server" />


... and here's the code-behind:

C#
protected void SearchTerms_Changed(object sender, EventArgs e)
{
    DropDownList thisDdl = (DropDownList)sender;
    string id = thisDdl.ID;
    string newVal = thisDdl.SelectedItem.Text;

    Session["Pass"] = newVal;
}


When I run this through the debugger and break on setting the session variable, ID contains the correct DropDownList ID, but the SelectedItem.Text always belongs to the first entry in the DropDownList. There are no duplicated items. Does anyone know why?
Posted
Comments
Wombaticus 25-Aug-14 14:25pm    
Are you re-binding your dropdownlist items in page_Load on postback? It should work otherwise.

1 solution

Bind your DropDown in !Page.IsPostBack property.
C#
private void Page_Load()
{
    if (!IsPostBack)
    {
        // Bind dropdwon here..
    }
}



--Amy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900