i created an ASP.NET Website. What i want to do is to make a label change its content depending on the item selected by a drop down list. I tried this but it didn't work:
The Drop down list looks like this:
<asp:DropDownList ID="DropDown1" runat="server" >
<asp:ListItem Value="a"></asp:ListItem>
<asp:ListItem Value="b"></asp:ListItem>
onselectedindexchanged="DropDown1_SelectedIndexChanged"
</asp:DropDownList>
the label:
<asp:Label ID="Label1" Text="" runat="server"/>
I want to do it without having to use PostBack.
I tried to use ajax Update panel Like this:
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="DropDown1" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" Text="" runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
And in the DropDown1_SelectedIndexChanged Event in the code behind:
protected void DropDown1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDown1.SelectedValue;
}
But this is not working.
Can anyone Help me with it?
Thank you very much for any help
Here is your solution..
replace your dropdown aspx control with below one..
<asp:DropDownList ID="DropDown1" runat="server" onselectedindexchanged="DropDown1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="a"></asp:ListItem>
<asp:ListItem Value="b"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" Text="test" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="DropDown1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
You need to enable autopostback and put the event handler definition in the right place:
<asp:DropDownList ID="DropDown1" runat="server" onselectedindexchanged="DropDown1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="a"></asp:ListItem>
<asp:ListItem Value="b"></asp:ListItem>
</asp:DropDownList>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With