C# 下拉菜单OnSelectedIndexChanged未启动

C# 下拉菜单OnSelectedIndexChanged未启动,c#,asp.net,drop-down-menu,autopostback,selectedindexchanged,C#,Asp.net,Drop Down Menu,Autopostback,Selectedindexchanged,未为我的下拉框触发选择的索引更改事件。我看过的所有论坛都告诉我添加AutoPostBack=“true”,但这并没有改变结果 HTML: 对于一个新手asp程序员,有什么建议吗 编辑:添加了更多代码 格雷厄姆·克拉克需要是正确的!IsPostBack,但现在我设置了全局变量。这段代码是从一个c#项目中拖拽出来的,因此我假设全局变量和asp.net存在一些问题。是时候让我在这方面做更多的研究了,以了解全局变量在独立程序和web程序中是如何不同的 您是在每次返回服务器时对下拉列表进行数据绑定,还是

未为我的下拉框触发选择的索引更改事件。我看过的所有论坛都告诉我添加
AutoPostBack=“true”
,但这并没有改变结果

HTML:

对于一个新手asp程序员,有什么建议吗

编辑:添加了更多代码



格雷厄姆·克拉克需要
是正确的!IsPostBack
,但现在我设置了全局变量。这段代码是从一个c#项目中拖拽出来的,因此我假设全局变量和asp.net存在一些问题。是时候让我在这方面做更多的研究了,以了解全局变量在独立程序和web程序中是如何不同的

您是在每次返回服务器时对下拉列表进行数据绑定,还是仅仅在回发时进行数据绑定?如果您每次都这样做,可能是服务器认为没有选择任何内容,因此事件不会触发

假设您正在对Page_Load事件中的下拉列表进行数据绑定。您希望这样做:

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

你确定它没有开火吗?您是否在foreach外部设置了断点?这可能是一个不相关的问题,让你相信它没有开火。你是如何将数据绑定到下拉列表的?您的标记显示一个空下拉列表。您是否在代码背后将值绑定到它?@matthew:是的,它不会在foreach循环之外触发@KP:我正在代码隐藏中设置时区信息。动态添加时区时也不会触发。
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);
        }
      }
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // bind drop-down list here
    }
}