Thursday, November 26, 2009

How to Set JS Path on Master Page for different level of content pages


1) Add a method in master page that include a script tag in header of page, like this: The two basic ways to solve this problem are:

Add a method in master page that include a script tag in header of page, like this:

private void addScriptInc(string path)
{
HtmlGenericControl script = new HtmlGenericControl(""script""); script.Attributes.Add(""type"", ""text/javascript""); script.Attributes.Add(""language"", ""javascript""); script.Attributes.Add(""src"", ResolveClientUrl(path)); Page.Header.Controls.Add(script);
}

or use the Page.ClientScript.RegisterClientScriptInclude method to add a script include inside body>form tags (i don't like this method)

2) Write the script tag like this:


The two solutions above can be used with other elements, like links, images, etc.

3) Register Script from server side.
String logOutJSPath = VirtualPathUtility.MakeRelative(Page.AppRelativeVirtualPath, ""~/JS/Logout.js"");
String logOut = """";
Page.ClientScript.RegisterStartupScript(this.GetType(), ""Logout"", logOut);"

No comments: