Change label text without PostBack (using update panels)

I created an ASP.NET website. I want to make the label change its contents depending on the item selected in the drop-down list. I tried this, but this did not work:

The dropdown list is as follows:

<asp:DropDownList ID="DropDown1" runat="server" > <asp:ListItem Value="a"></asp:ListItem> <asp:ListItem Value="b"></asp:ListItem> onselectedindexchanged="DropDown1_SelectedIndexChanged" </asp:DropDownList> 

label:

 <asp:Label ID="Label1" Text="" runat="server"/> 

I want to do this without using PostBack .

I tried using the ajax Refresh panel.

 <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 codec :

 protected void DropDown1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDown1.SelectedValue; } 

But that does not work.

Can anybody help me?

Thanks so much for any help

+6
source share
3 answers

Here is your decision ..

replace the aspx dropdown control below.

  <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> 
+9
source

You need to enable autorun 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> 
+5
source

http://encosia.com/why-aspnet-ajax-updatepanels-are-dangerous/

Read this article on why NOT using updates; there are many other better solutions for completing a task.

0
source

Source: https://habr.com/ru/post/897380/


All Articles