10

I have a feeling I'm missing something really obvious, I'm not able to capture the selected value of my DropDownList; the value renaubs the first item on the list. I have set the DropListList autopostback property to true. I have a SelectedIndexChangedEvent which is pasted below. This is NOT on the master page.

protected void ddlRestCity_SelectedIndexChanged(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        r_city = ddlRestCity.SelectedValue.ToString();
    }
}

Here is the DropDownList control:

<asp:DropDownList ID="ddlRestCity" runat="server" 
        Width="100px" AutoPostBack="True" 
        onselectedindexchanged="ddlRestCity_SelectedIndexChanged">
</asp:DropDownList>

Thanx in advance for your help!

Susan
  • 1,702
  • 8
  • 43
  • 67

3 Answers3

12

My off the cuff guess is you are maybe re-populating the list on a post back and that is causing the selected index to get reset.

Felan
  • 1,263
  • 10
  • 17
8

Where is your DataBind() call? Are you checking !IsPostBack before the call? For example:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        ddlRestCity.DataSource = ...;
        ddlRestCity.DataBind();
    }
}

Explanation: If you don't check for !IsPostBack before DataBind(), the list will re-populate before SelectedIndexChanged is fired (because Page.Load fires before child events such as SelectedIndexChanged). When SelectedIndexChanged is then fired, the "selected item" is now the first item in the newly-populated list.

Justin M. Keyes
  • 5,851
  • 1
  • 28
  • 57
0

What is r_city?

If it's a textbox, then you need to do something like r_city.text = ...

Also -- you might consider removing your postback check. Usually, that's most useful in the page.onload event, and usually, you're checking for if NOT ispostback...

Chains
  • 11,681
  • 8
  • 40
  • 61