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