Listbox causing a separate postback for the textbox

145 Views Asked by At

Step to replicate the problem

First, type in text in the textbox but do not type enter. Then, select a different index in the listbox. Finally, view that both ID ( listbox and textbox ) will show up at the eventtarget.

expected result Only one ID will show up at the eventtarget, which is the one that initialize it = listbox onselected index change.

Here's the code:

<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

There are 1 best solutions below

0
On

If your text box has its AutoPostBack property set to true, the text box will cause a PostBack when it loses focus. This is by design. See the docs.

Therefore, you getting the ID of the text box there is actually the expected behavior. If you don't want that, consider not setting the text box AutoPostBack property to true.