質問

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コーダーを何を見るかについてのアドバイスはありますか?

編集: :後ろにコードを追加しました


グラハム・クラークは必要とするのが正しかった !Page.IsPostBack, 、しかし、それは私が設定したグローバル変数を持つものです。このコードはドラッグされ、AC#プロジェクトからドロップされたため、グローバル変数とASP.NETにいくつかの問題があると思います。 Webプログラムとは対照的に、スタンドアロンでグローバル変数がどのように異なるかを理解するために、これについてさらに調査する時間です。

役に立ちましたか?

解決

サーバーに戻るたびに、または単にポストバック上にドロップダウンリストをデータバインドしていますか?毎回それをしている場合、サーバーは何も選択されていないと考えていないため、イベントは発生しません。

Page_loadイベントでドロップダウンをデータバインドしているとします。あなたはこのようにそれをしたい:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // bind drop-down list here
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top