Asked by:
ASP Form Dropdown OnSelectedIndexChanged event not working

Question
-
User2037455357 posted
Hello
I have dropdown OnselectionIndexChanged event
I want to change a label backcolor based on the Dropdown.SelectItem.value.
I have AutoPostBack="true" in the dropdown aspx
If I have no AutoPostBack in the aspx the c# event does not fire.
but when I go through the code on debug the selection is always the top value which I assume is due to the postback = true.
C# below.
if (DropDown.SelectedItem.Value == "value3") { Lbl.BackColor = Color.Red; Lbl.Text = "High"; } else { Lbl.BackColor = Color.Green; Lbl.Text = "Low"; }
But when I select the 3rd value the if statement always skips the condition and goes onto the else statement.
what am I doing wrong please
Regards
Rob
Sunday, May 19, 2019 3:14 PM
All replies
-
User2037455357 posted
Its ok, sorted it,
I wrapped my code within a isPostBack to cope with postbacks,
see below.
if (IsPostBack) return; { if (DropDown.SelectedItem.Value == "value3") { Lbl.BackColor = Color.Red; Lbl.Text = "High"; } else { Lbl.BackColor = Color.Green; Lbl.Text = "Low"; } }
Sunday, May 19, 2019 5:22 PM -
User288213138 posted
Hi masterdineen,
Have you solved your problem?
I tried to reproduce your problem and found that when I select 3rd value, the if statement was working normally.
Please check your code, if there are still problems, Please elaborate on your question.
The Reslut:
Monday, May 20, 2019 2:46 AM -
User2037455357 posted
Hi. Sameu<br>
<br>
I see you had the same result with or without using ispostback. i’m not sure how i managed to get
<br>
the incorrect result with my version. i know if i didn’t include postback: auto within the aspx code the event didn’t even fire off.
<br>
<br>
did you have postback: Auto within the dropdown aspx as well ?
Also if i didn’t use ispostback in the page load the dropdown selection would always
default to the first value in the dropdownMonday, May 20, 2019 4:33 AM -
User-893317190 posted
Hi masterdineen,
Sam's code shows that no matte you write return or not in pageload , the selectedindexchange event will always fire.
Not sure why your event couldn't fire , a normal wring is
<form id="form1" runat="server" > <asp:DropDownList ID="DropDown" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem >value3</asp:ListItem> <asp:ListItem >value2</asp:ListItem> <asp:ListItem >value1</asp:ListItem> </asp:DropDownList> <asp:Label ID="Lbl" runat="server" Text="Label"></asp:Label> </form>
Code behind.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (DropDown.SelectedItem.Value == "value3") { Lbl.BackColor = Color.Red; Lbl.Text = "High"; } else { Lbl.BackColor = Color.Green; Lbl.Text = "Low"; } }
Could you show your complete code? I am afraid your other code causes this problem.
Best regards,
Ackerly Xu
Tuesday, May 21, 2019 2:44 AM -
User2037455357 posted
this IS working now,
and is the same as your so thank you for your help
Tuesday, May 21, 2019 6:39 AM