下拉选择更改了事件未触发 [英] dropdown selection changed event not firing

查看:55
本文介绍了下拉选择更改了事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中有下拉列表并在加载事件上填充值。选择更改事件未在下拉选项更改时触发。



I have dropdown in asp.net and populating value on load event.Selection Changed event is not firing on drop down selection changed .

<pre lang="HTML">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <asp:DropDownList ID="dropdown1" OnInit="OnInit" ViewStateMode=Enabled runat=server OnSelectedIndexChanged="DropDownChanged"  AutoPostBack=true EnableViewState=true>
    
    </asp:DropDownList>
   
    </div>
    </form>
</body>





这里是.cs文件



and here is .cs file

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        dropdown1.Items.Add("a");
        dropdown1.Items.Add("a");
        dropdown1.Items.Add("a");
        dropdown1.Items.Add("a");
        dropdown1.Items.Add("a");
        dropdown1.Items.Add("a");
    }

}
protected void DropDownChanged(object sender, EventArgs e)
{
    Response.Write(dropdown1.SelectedItem.Text.ToString());
}







不知道有什么问题.....请帮助.......我缺少什么




Dont know what is wrong..... please help ....... what i am missing

推荐答案

客户端正在使用值(在您的示例中为所有的)来查看索引是否具有改变。因为它们都是相同的值,所以客户端从不调用已更改的事件。



有两个选项,更改代码以使dropdown1值全部不同。像这样...

The client side is using the values (all a's in your example) to see if the index has changed. Because they are all the same values the client side never calls the changed event.

There are two options, change your code so the dropdown1 values are all different. Like this...
dropdown1.Items.Add("a");
dropdown1.Items.Add("b");
dropdown1.Items.Add("c");
dropdown1.Items.Add("d");
dropdown1.Items.Add("e");
dropdown1.Items.Add("f");



或者创建一个ListItem并将项目值与文本一起设置。然后,您可以使用相同的文本值并触发客户端调用,因为它将使用项值来区分项。像这样...


Or create a ListItem and set the item value along with the text. Then you can have the same text value and trigger the client call because it will use the item value to differentiate the items. Like this...

ListItem li = new ListItem("a", "1");
dropdown1.Items.Add(li);
li = new ListItem("a", "2");
dropdown1.Items.Add(li);
li = new ListItem("a", "3");
dropdown1.Items.Add(li);
li = new ListItem("a", "4");
dropdown1.Items.Add(li);
li = new ListItem("a", "5");
dropdown1.Items.Add(li);
li = new ListItem("a", "6");
dropdown1.Items.Add(li);


这篇关于下拉选择更改了事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆