我正在动态创建一个包含两个下拉列表的表。我想在选择任一 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);
}