Dropdown OnSelectedIndexChanged not firing

Jim picture Jim · Jun 8, 2010 · Viewed 27.9k times · Source

The OnSelectedIndexChanged event is not firing for my dropdown box. All forums I have looked at told me to add the AutoPostBack="true", but that didn't change the results.

HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label1" runat="server" Text="Current Time:  " /><br />
      <asp:Label ID="lblCurrent" runat="server" Text="Label" /><br /><br />
      <asp:DropDownList ID="cboSelectedLocation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboSelectedLocation_SelectedIndexChanged"  /><br /><br />
      <asp:Label ID="lblSelectedTime" runat="server" Text="Label" />
    </div>
    </form>
</body>
</html>

Code behind:

public partial class _Default : Page 
{
    string _sLocation = string.Empty;
    string _sCurrentLoc = string.Empty;
    TimeSpan _tsSelectedTime;

    protected void Page_Load(object sender, EventArgs e)
    {
      AddTimeZones();
      cboSelectedLocation.Focus();
      lblCurrent.Text = "Currently in " + _sCurrentLoc + Environment.NewLine + DateTime.Now;
      lblSelectedTime.Text = _sLocation + ":" + Environment.NewLine + DateTime.UtcNow.Add(_tsSelectedTime);
    }

    //adds all timezone displaynames to combobox
    //defaults combo location to seoul, South Korea
    //defaults current location to current location
    private void AddTimeZones()
    {
      foreach(TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
      {
        string s = tz.DisplayName;
        cboSelectedLocation.Items.Add(s);
        if (tz.StandardName  == "Korea Standard Time") cboSelectedLocation.Text = s;
        if (tz.StandardName == System.TimeZone.CurrentTimeZone.StandardName) _sCurrentLoc = tz.StandardName;
      }
    }

    //changes timezone name and time depending on what is selected in the cbobox.
    protected void cboSelectedLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
      foreach (TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
      {
        if (cboSelectedLocation.Text == tz.DisplayName)
        {
          _sLocation = tz.StandardName;
          _tsSelectedTime = tz.GetUtcOffset(DateTime.UtcNow);
        }
      }
    }
}

Any advice into what to look at for a rookie asp coder?

EDIT: added more code behind


Graham Clark was correct in needing the !Page.IsPostBack, but it is now something with the global variables that I set. This code was dragged and dropped from a c# project, so I assume there is some issues with global variables and asp.net. Time for me to do more research on this to understand how global variables differ in a standalone as opposed to a web program.

Answer

Graham Clark picture Graham Clark · Jun 8, 2010

Are you databinding your drop-down list every trip back to the server, or just on a postback? If you're doing it every time, it could be that the server doesn't think anything has been selected, therefore the event won't fire.

Say you're databinding the drop-down in the Page_Load event. You want to do it like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // bind drop-down list here
    }
}