javascript 如何在 C# 代码隐藏中将 OnClientClick 添加到 DDL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11198989/
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
How to add OnClientClick to DDL in c# codebehind
提问by Bob Jones
I am dynamically creating a table that contains two drop down lists. I want to fire an OnClientClick event to execute some JavaScript when either DDL is selected, but don't see a way to add an OnClientClick to a DDL. Here is the code as it currently sits. I tried adding the OnClientClick to the Item, but it isn't working.
我正在动态创建一个包含两个下拉列表的表。我想在选择 DDL 时触发 OnClientClick 事件以执行一些 JavaScript,但没有看到将 OnClientClick 添加到 DDL 的方法。这是当前所在的代码。我尝试将 OnClientClick 添加到项目,但它不起作用。
HtmlTableCell tableCell = new HtmlTableCell();
tableCell.Attributes.Add("class", cssPageGroups);
DropDownList ddlPageGroups = new DropDownList();
ddlPageGroups.Attributes.Add("class", cssPageGroupsDDL);
ddlPageGroups.ID = "ddlPageGroups";
ddlPageGroups.AutoPostBack = true;
ddlPageGroups.SelectedIndexChanged += new EventHandler(pageGroupChange);
for (int pg = 1; pg <= maxPageGroups; pg++)
{
int groupFirstPageNumber = (int)(1 + (maxVisiblePageNumbers * (pg - 1)));
int groupLastPageNumber = groupFirstPageNumber + (maxVisiblePageNumbers - 1);
if (totalPages < groupLastPageNumber)
{
groupLastPageNumber = totalPages;
}
string group = String.Format("{0} ... {1}", groupFirstPageNumber.ToString(), groupLastPageNumber.ToString());
ListItem groupItem = new ListItem(group, ((groupFirstPageNumber - 1) * pageSize).ToString());
if (pageGroup == pg)
{
groupItem.Selected = true;
}
groupItem.Attributes.Add("OnClientClick", "javascript:showSearching();");
ddlPageGroups.Items.Add(groupItem);
}
回答by xandercoded
Change:
改变:
ddlPageGroups.Attributes.Add("OnClientClick", "javascript:showSearching();");
To:
到:
ddlPageGroups.Attributes.Add("onclick", "showSearching();");
Remember, the Attributes collection is adding HTMLattributes to the select list. OnClientClick
is a server-side
attribute for the DropDownList
.
请记住,Attributes 集合将HTML属性添加到选择列表中。OnClientClick
是 的server-side
属性DropDownList
。
回答by andleer
You have to add the onclick event to the select tag. It will not work on the individual option tags.
您必须将 onclick 事件添加到选择标签。它不适用于单个选项标签。