1

EDIT: Solved. An attempt to bind a DataTable to the DDL accidentally happened twice once while not checking the page's PostBack status. Once the redundant attempt to bind was removed the problem went away. Thanks for the assistance everyone.

I have an aspx page and some code behind that feature a drop down list (ddlLocationState) or US states in alphabetical order. However no matter what item is selected, if I do ddlLocationState.SelectedItem/Value/Index it's always "Alaska" and "0". Every single time. Even in the SelectedIndexChange event handler.

Here's where the DDL is declared in the aspx page (this is the only reference to it in the page):

<tr>
   <td>&nbsp;</td>
   <td class="textBlue"><label>State: </label></td>
   <td>
      <asp:DropDownList runat="server" ID="ddlLocationState" style="width: 250px;"/>
   </td>
</tr>

Here's the code where the contents of the DDL are setup up: private void BindStates()

  private void BindDDL()
  {
     //Get all state info:
     // Populate a DataTable called "sdt" with two columns, name (a state)
     // and "id" (a number from 0 to 49)

     //Bind the DataTable "sdt":
     this.ddlLocationState.DataSource = sdt;
     this.ddlLocationState.DataTextField = "name";
     this.ddlLocationState.DataValueField = "id";
     this.ddlLocationState.DataBind();
  }

Here's some code-behind trying to get the currently selected value:

private void ddlLocationState_SelectedIndexChanged(object sender, EventArgs e)
{
   System.Diagnostics.Debug.WriteLine("on index changed:   " +  dlLocationState.SelectedItem.Value.ToString());
}

Here's where BindDDL is called:

protected void Page_Load(object sender, EventArgs e)
      {
         this.pnlLocationMaintenance.Visible = false;
         this.LocationSortDirection = "asc";
         this.LocationSortField = "address";
         this.BindStates();
         this.dgLocations.CurrentPageIndex = 0;
         this.BindLocations();

         if (!Page.IsPostBack)
         {
            BindDDL();
         }
         ddlLocationState.SelectedIndexChanged += new EventHandler(ddlLocationState_SelectedIndexChanged);
      }

Why is the ddlLocationState.SelectedItem/Value/Index always the same, even in the selected index change event handler?

4
  • 1
    are you calling ` BindDDL()` frome page load. If yes then please put it in if(!postBack). Regards Commented Feb 10, 2012 at 13:56
  • @Hayer Yeah, I just tried that and see the same behavior. Also it seems like the select index change isn't even firing. I've definitely wired this event in Page_Load...truly weird. Commented Feb 10, 2012 at 14:00
  • post the code where BindDDL is called. Commented Feb 10, 2012 at 14:02
  • The try deleting ddlLocationState_SelectedIndexChanged and recreate it by double clicking on Dropdown Control in design mode. I hope it will work. Commented Feb 10, 2012 at 14:12

3 Answers 3

2

This will happen if you are databinding on page_load without checking to see whether you are in a postback state or not. It is my assumption from your described symptoms that this is what is happening.

In your PageLoad(...) method you need to: if (!Page.IsPostBack) { BindDDL(); }

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advice. I tried this to no avail. One thing I forgot to mention was that the binding seems to work. I.e. the DDL populates with all the text from the DataTable "sdt."
This was it after all. I accidentally did the binding of DDL twice, once while not checking page PostBack status. That was the problem. Once the other attempt to bind DDL was removed everything worked. Thanks!
2

Show us the code where you call BindDDl(). My guess is that you are calling BindDDl() even on postback, so it repopulates the list and clears your selection.

1 Comment

I tried to bind the DDL twice, once while not checking the postback status. I removed this redundant piece of code and all is well. Thanks!
2

Are you calling the BindDDL() method at page_load event? if so you should check if the request is not a post back

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.