2013-08-28 27 views
0

複製問題的步驟導致文本框單獨回發的列表框

首先,在文本框中輸入文本但不輸入回車。 然後,在列表框中選擇一個不同的索引。 最後,查看ID(列表框和文本框)將顯示在事件目標處。

預期結果 只有一個ID會顯示在eventtarget上,它是初始化它的那個ID = listbox onselected index change。

下面的代碼:

<TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox> <ListBox ID="ListBox1" runat="server" AutoPostBack="True" onselectedindexchanged="ListBox1_SelectedIndexChanged"> <asp:ListItem Text="0" Value="0"></asp:ListItem> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> </ListBox> protected void Page_Load(object sender, EventArgs e){ if (IsPostBack) { string target = Request["__EVENTTARGET"] as string; System.Windows.Forms.MessageBox.Show("__EVENTTARGET: " + target); } } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { }

回答

0

如果你的文本框有其AutoPostBack屬性設置爲true,文本框會導致回發時,它失去焦點。這是設計。 See the docs

因此,您獲取文本框的ID實際上是預期的行爲。如果你不想這樣做,請考慮不要將文本框AutoPostBack屬性設置爲true