RegisterClientScriptBlock

I saw this recent MSDN TV episode about ASP.NET client side script handling. As a sworn hater of client side scripts I played the clip in horror, but found that as long as I can assign these scrips to my pages serverside (and dynamically) they are actually pretty cool.

The first use I got for this snippet was for dynamically assigning CSS templates to pages based on user preferences:

In PreRender (or Init):

this.RegisterClientScriptBlock("css", "<link href='"+ myVariableWithCSSFilename +"' rel=stylesheet/>");

2 Comments

  • I really haven't tested this in Mozilla. But you're right that it's generated inside the form tag.





    The alternative solution is to use a regular link tag like this:





    &lt;link rel=&quot;&lt;%# getCss() %&gt;&quot;&gt; and let a method called codeBehindClassName.getCss() return the path. What to watch out for here is that you have to do a Page.Databind() explicitly in Load.

  • You could also place a literal control and use the codebehind for that to output your css link tag

Comments have been disabled for this content.