列表框导致文本框单独回发

时间:2013-08-28 17:48:03

标签: c# textbox listbox autopostback

重复问题的步骤

首先,在文本框中键入文本但不输入enter。 然后,在列表框中选择其他索引。 最后,查看ID(列表框和文本框)将显示在eventtarget。

预期结果 只有一个ID会显示在eventtarget上,即初始化它= listbox onselected index changes。

这是代码:

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

1 个答案:

答案 0 :(得分:0)

如果您的文本框的AutoPostBack属性设置为true,则文本框会在失去焦点时导致PostBack。这是设计的。 See the docs

因此,您获得文本框的ID实际上是预期的行为。如果您不想这样做,请考虑不要将文本框AutoPostBack属性设置为true

相关问题