c# - 下拉 OnSelectedIndexChanged 未触发

标签 c# asp.net drop-down-menu autopostback selectedindexchanged

OnSelectedIndexChanged 事件没有为我的下拉框触发。我查看过的所有论坛都告诉我添加 AutoPostBack="true",但这并没有改变结果。

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>

代码隐藏:

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);
        }
      }
    }
}

有什么关于新手 asp 编码员应该注意什么的建议吗?

编辑:在后面添加了更多代码


Graham Clark 需要 !Page.IsPostBack 是正确的,但它现在是我设置的全局变量。此代码是从 c# 项目中拖放的,因此我假设全局变量和 asp.net 存在一些问题。我是时候对此进行更多研究,以了解全局变量在独立程序和 Web 程序中有何不同。

最佳答案

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

假设您在 Page_Load 事件中对下拉列表进行数据绑定(bind)。你想这样做:

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

关于c# - 下拉 OnSelectedIndexChanged 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2998393/

相关文章:

c# - 为什么我在 SignalR 上收不到以复杂对象作为参数的事件?

c# - 在 SVG 文件中获取 XML 元素时出现问题

asp.net - 想要选择像 asp.net 或 stackoverflow 这样的标签,例如?

c# - 在 Medium Trust 中调用内部方法

java - Selenium - 使用通配符从下拉列表中选择一个选项?

html - 下拉菜单-父链接跳转,子链接偏离中心

c# - 有没有办法使用 FileHelpers 库进行现场排序?

asp.net - asp按钮上的JS回调

asp.net - 在 asp.net 下拉列表中,某些值被禁用以选择

c# - Where() 的意外结果