Wednesday, 28 August 2013

Listbox causing a separate postback for the textbox

Listbox causing a separate postback for the textbox

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

No comments:

Post a Comment