3

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jim
  • 3,425
  • 9
  • 32
  • 49
  • 2
    Are you sure it is not firing? Have you set a breakpoint outside of the foreach? It could be an unrelated problem that leads you to believe it is not firing. – ctorx Jun 08 '10 at 14:56
  • 2
    How are you binding data to the dropdown? Your markup shows an empty dropdown. Are you binding values to it in the code behind? – KP. Jun 08 '10 at 14:59
  • @matthew: Yes it does not fire outside of the foreach loop. @KP: I am setting timezone info in the code behind. – Jim Jun 08 '10 at 15:04
  • It also does not fire when adding timezones dynamically. – Jim Jun 08 '10 at 15:06

1 Answers1

10

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
    }
}
Graham Clark
  • 12,886
  • 8
  • 50
  • 82