C# html 选择(下拉)控件在asp.net中选择索引更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16112218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:03:12  来源:igfitidea点击:

html select (dropdown) control selected index changed event in asp.net

c#asp.netdrop-down-menuwebforms

提问by Laziale

I am trying to put backend code to my html select control (dropdown) when the value is changed a backend method to be triggered, but I Can't find the event. I tried this way:

当值更改为要触发的后端方法时,我试图将后端代码放入我的 html 选择控件(下拉列表),但我找不到该事件。我试过这种方式:

<select id="ddlCompany" name="select2" onchange="ddlCompany_SelectedIndexChanged" runat="server" class="dropdown nostyle sel1" style="width:100%;" placeholder="Select Company" />

nothing changes. Can anyone advice how I can fix this? Thx, Laziale

没有什么改变。谁能建议我如何解决这个问题?谢谢,拉齐亚莱

采纳答案by nikeaa

Try using the SelectedIndexChanged property instead of onchange, like this:

尝试使用 SelectedIndexChanged 属性而不是 onchange,如下所示:

<select id="ddlCompany" name="select2" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged" runat="server" class="dropdown nostyle sel1" style="width:100%;" placeholder="Select Company" />

回答by The Vanilla Thrilla

Add this to your code behind:

将此添加到您的代码后面:

protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
{
     //code here
}

And this to your markup:

这对您的标记:

OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged" AutoPostBack="True"

回答by Shyju

If you are using the dropdownlist server control. Go to the designer view, Select the dropdownlist server control and right click and select Properties. Now in the properties window, click on the Eventsicon and then you can see the SelectedIndexChangedevent. Double click on the blank white space on the right side and Visual studio will generate the relevant code for you.

如果您使用下拉列表服务器控件。转到设计器视图,选择下拉列表服务器控件并右键单击并选择Properties。现在在属性窗口中,单击“事件”图标,然后您就可以看到该SelectedIndexChanged事件。双击右侧空白处,Visual Studio 将为您生成相关代码。

enter image description here

在此处输入图片说明