最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add OnClientClick to DDL in c# codebehind - Stack Overflow

programmeradmin3浏览0评论

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.

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);
}

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.

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);
}
Share Improve this question asked Jun 25, 2012 at 23:49 Bob JonesBob Jones 2,0485 gold badges33 silver badges60 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Change:

ddlPageGroups.Attributes.Add("OnClientClick", "javascript:showSearching();");

To:

ddlPageGroups.Attributes.Add("onclick", "showSearching();");

Remember, the Attributes collection is adding HTML attributes to the select list. OnClientClick is a server-side attribute for the DropDownList.

You have to add the onclick event to the select tag. It will not work on the individual option tags.

发布评论

评论列表(0)

  1. 暂无评论