下拉 OnSelectedIndexChanged 未触发

发布于 2024-09-04 07:45:29 字数 2624 浏览 7 评论 0原文

我的下拉框没有触发 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,但现在它是我设置的全局变量的东西。该代码是从 ac# 项目中拖放的,因此我认为全局变量和 asp.net 存在一些问题。我是时候对此进行更多研究,以了解全局变量在独立程序与 Web 程序中有何不同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2024-09-11 07:45:29

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

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

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

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文