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/>");

Filed under:

Comments

# FnaD said:

that works? according to the MSDN doco (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIPageClassRegisterClientScriptBlockTopic.asp) the "script" is emitted after the start of the FORM tag, *not* in the head of the document. Does Mozilla handle a link rel *inside* the body of the document? I'd love to know, coz that sounds funky!

Monday, March 24, 2003 8:05 AM
# Mads said:

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:

<link rel="<%# getCss() %>"> 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.

Monday, March 24, 2003 8:36 AM
# Wee Bubba said:

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

Friday, April 30, 2004 4:42 AM