<%@ Master Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
jQueryClientIdEnhancement.RegisterExtScript(this.Page);
}
//You can move this class to App_Code to be used by all MasterPages
public class jQueryClientIdEnhancement
{
public static void RegisterExtScript(Page page)
{
Dictionary<string, string> dct = new Dictionary<string, string>();
//explore all containers to find all visible webcontrols
searchContentPlaceHolder(page.Form, dct);
StringBuilder sb = new StringBuilder();
foreach (string key in dct.Keys)
{
if (sb.Length > 0) sb.Append(",");
//key = ClientId, value = Id
sb.AppendFormat("{0}:\"{1}\"", key, dct[key]);
}
//build the hashtable
string script = @"window.aspNetWebControls = {" + sb.ToString() + "};\n";
//assign the assistant class name to each web control's HTML element
script += @"
if (typeof(jQuery) == 'function' && window.aspNetWebControls) {
var c = window.aspNetWebControls;
for (var clientId in c)
$('#' + clientId).addClass('_' + c[clientId]);
var pattern = /##(\w+)/g;
window.$$ = function( selector, context ) {
selector = selector.replace(pattern, '._$1');
return jQuery(selector, context);
}
}
";
//put the script at the end of form to make sure every webcontrol
//element is declared
page.ClientScript.RegisterStartupScript(page.GetType(),
"jQueryClientIdEnhancement",
script, true);
}
private static void searchContentPlaceHolder(Control ctrl,
Dictionary<string, string> dct)
{
if (ctrl.HasControls())
foreach (Control c in ctrl.Controls)
searchContentPlaceHolder(c, dct);
if (ctrl.Visible)
dct.Add(ctrl.ClientID, ctrl.ID);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
</html>