tahasozgen tahasozgen - 1 month ago 5x
ASP.NET (C#) Question

Asp.Net label does not update in drop down list's selection index changes

I have a basic drop down list. Upon selection of an item in the list, I expect to update the label with the choice of drop down list. But my code section does not show any update. Here is the code:


protected void Page_Load(Object sender, EventArgs e)
if (!this.IsPostBack)
// Use integers to index each item. Each item is a string.
Dictionary<int, string> fruit = new Dictionary<int, string>();
fruit.Add(1, "Kiwi");
fruit.Add(2, "Pear");
fruit.Add(3, "Mango");
fruit.Add(4, "Blueberry");
fruit.Add(5, "Apricot");
fruit.Add(6, "Banana");
fruit.Add(7, "Peach");
fruit.Add(8, "Plum");
// Define the binding for the list controls.
benimDropDownList.DataSource = fruit;
// Choose what you want to display in the list.
benimDropDownList.DataTextField = "Value";
// Activate the binding.

protected void benimDropDownList_SelectedIndexChanged(object sender, EventArgs e)
lblSonuc.Text = "You picked: " + benimDropDownList.SelectedItem.Text;
lblSonuc.Text += " which has the key: " + benimDropDownList.SelectedItem.Value;


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">

<asp:DropDownList ID="benimDropDownList" runat="server" OnSelectedIndexChanged="benimDropDownList_SelectedIndexChanged">

<asp:Label ID="lblSonuc" runat="server"></asp:Label>


What is wrong with it?


Add AutoPostBack="true" to your <asp:DropDownList>:

<asp:DropDownList ID="benimDropDownList" runat="server" 
    OnSelectedIndexChanged="benimDropDownList_SelectedIndexChanged" AutoPostBack="true">

By default <asp:DropDownList> do not post back changes to the server. Thats why the event is never called.