Blog Stats
  • Posts - 178
  • Articles - 0
  • Comments - 136
  • Trackbacks - 172

 

MasterPages Dynamic Script Include

I had a bit of trouble figuring this one out. I needed to include a script in a MasterPage dynamically based on the url/path of the current page. Eventually, I found the ClientScriptManager.RegisterClientScriptInclude method. Here is a short snippet:

if (Request.RawUrl.ToLower().IndexOf("/client") > 0)
{
     cs.RegisterClientScriptInclude(
"ClientScripts", "../../Scripts/ClientScripts.js");
}
else if (Request.RawUrl.ToLower().IndexOf("/contacts") > 0)
{
     cs.RegisterClientScriptInclude(
"ContactScripts", "../../Scripts/ContactScripts.js");
}

That is all well and good but ClientScriptManager is the real jewel here. Anyone that has had to manage client scripts in ASP.NET 1.x knows what a pain it is and the ClientScriptManager makes it so easy. I can count at least a dozen instances it would have been gold to have it with 1.x.


Feedback

No comments posted yet.


Post a comment





 

 

 

Copyright © Jason Bentley