ASP.NET DropDownList SelectedIndexChanged事件不起作用
我有一个基本的下拉列表。在选择列表中的项目后,我希望使用下拉列表选项更新标签。但是我的代码部分没有显示任何更新。代码如下: MainPage.aspx.cs:ASP.NET DropDownList SelectedIndexChanged事件不起作用,asp.net,webforms,Asp.net,Webforms,我有一个基本的下拉列表。在选择列表中的项目后,我希望使用下拉列表选项更新标签。但是我的代码部分没有显示任何更新。代码如下: MainPage.aspx.cs: protected void Page_Load(Object sender, EventArgs e) { if (!this.IsPostBack) { // Use integers to index each item. Each item is a string. Dictiona
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.
this.DataBind();
}
}
protected void benimDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
lblSonuc.Text = "You picked: " + benimDropDownList.SelectedItem.Text;
lblSonuc.Text += " which has the key: " + benimDropDownList.SelectedItem.Value;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!this.IsPostBack)
{
//使用整数索引每个项目。每个项目都是一个字符串。
字典水果=新字典();
添加(1,“猕猴桃”);
水果。加入(2,“梨”);
水果。添加(3,“芒果”);
水果。加入(4,“蓝莓”);
水果。添加(5,“杏”);
水果。添加(6,“香蕉”);
添加(7,“桃”);
水果。添加(8,“李子”);
//定义列表控件的绑定。
benimDropDownList.DataSource=水果;
//选择要在列表中显示的内容。
benimDropDownList.DataTextField=“Value”;
//激活绑定。
这个.DataBind();
}
}
受保护的void benimDropDownList\u SelectedIndexChanged(对象发送方,事件参数e)
{
lblSonuc.Text=“您选择:”+benimDropDownList.SelectedItem.Text;
lblSonuc.Text+=”其键为:“+benimDropDownList.SelectedItem.Value;
}
MainPage.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="benimDropDownList" runat="server" OnSelectedIndexChanged="benimDropDownList_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lblSonuc" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
有什么问题吗?将AutoPostBack=“true”
添加到
:
默认情况下,不要将更改回发到服务器。将AutoPostBack=“true”
添加到
:
默认情况下,不要将更改回发到服务器。您可能还需要为dropdownlist设置DataValueField:
benimDropDownList.DataValueField = "Key";
您可能还希望为dropdownlist设置DataValueField:
benimDropDownList.DataValueField = "Key";
哦,我忘了。哦,我忘了。