【问题标题】:Listbox causing a separate postback for the textbox列表框导致文本框单独回发
【发布时间】:2013-08-28 17:48:03
【问题描述】:

复制问题的步骤

首先,在文本框中输入文本,但不要输入回车。 然后,在列表框中选择不同的索引。 最后,查看 ID( listbox 和 textbox )都将显示在 eventtarget 上。

预期结果 eventtarget 上只会显示一个 ID,即初始化它的 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) { }

【问题讨论】:

    标签: c# textbox listbox autopostback


    【解决方案1】:

    如果您的文本框将其AutoPostBack 属性设置为true,则文本框在失去焦点时将导致回发。这是设计使然。 See the docs.

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

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多