/// <summary>
/// Inserts a client script resource into the Html header of the page rather
/// than into the body as RegisterClientScriptInclude does.
///
/// Scripts references are embedded at the bottom of the Html Header after
/// any manually added header scripts.
/// </summary>
/// <param name="control"></param>
/// <param name="type"></param>
/// <param name="resourceName"></param>
/// <param name="comment"></param>
public void RegisterClientScriptResourceInHeader(Control control, Type type, string resourceName, string comment)
{
if (control.Page.Header == null)
{
this.RegisterClientScriptResource(control, type, resourceName);
return;
}
// *** Keep duplicates from getting written
const string identifier = "headerscript_";
if (HttpContext.Current.Items.Contains(identifier + resourceName))
return;
else
HttpContext.Current.Items.Add(identifier + resourceName, string.Empty);
object val = HttpContext.Current.Items["__ScriptResourceIndex"];
int index = 0;
if (val != null)
index = (int)val;
// *** Retrieve the Resource URL adjusted for MS Ajax, wwScriptCompression or stock ClientScript
string script = GetClientScriptResourceUrl(control.Page, typeof(ControlResources), resourceName);
// *** Embed in header
StringBuilder sb = new StringBuilder(200);
if (comment != null)
sb.AppendLine("<!-- " + comment + " -->");
sb.AppendLine(@"<script src=""" + script + @""" type=""text/javascript""></script>");
control.Page.Header.Controls.AddAt(index, new LiteralControl(sb.ToString()));
index++;
HttpContext.Current.Items["__ScriptResourceIndex"] = index;
}
/// <summary>
/// Registers a client script reference in the header instead of inside the document
/// before the form tag.
///
/// The script tag is embedded at the bottom of the HTML header.
/// </summary>
/// <param name="control"></param>
/// <param name="type"></param>
/// <param name="Url"></param>
/// <param name="comment"></param>
public void RegisterClientScriptIncludeInHeader(Control control, Type type, string Url, string comment)
{
if (control.Page.Header == null)
{
this.RegisterClientScriptInclude(control, type, Url, Url);
return;
}
// *** Keep duplicates from getting written
const string identifier = "headerscript_";
if (HttpContext.Current.Items.Contains(identifier + Url.ToLower()))
return;
else
HttpContext.Current.Items.Add(identifier + Url.ToLower(), string.Empty);
// *** Retrieve script index in header
object val = HttpContext.Current.Items["__ScriptResourceIndex"];
int index = 0;
if (val != null)
index = (int)val;
// *** Embed in header
StringBuilder sb = new StringBuilder(200);
if (comment != null)
sb.AppendLine("<!-- " + comment + " -->");
sb.AppendLine(@"<script src=""" + Url + @""" type=""text/javascript""></script>");
control.Page.Header.Controls.AddAt(index, new LiteralControl(sb.ToString()));
index++;
HttpContext.Current.Items["__ScriptResourceIndex"] = index;
}