ScriptOnly - The Opposite of a NOSCRIPT
There’s a lot of ways that this can be accomplished, but one of the more popular ways is with the SCRIPT/NOSCRIPT combo...
While this works fine in a lot of scenarios, it can get especially tricky when you want to put server-side controls on the SCRIPT side of things. A lot of developers resort to something like this...
<asp:LinkButton runat="server" ... />
<asp:Button runat="server" ... />
... and of course, things quickly get much uglier once you do this in the real world.
One solution that I use is a simple, custom-control called ScriptOnly. It works just like this...
<asp:LinkButton runat="server" onClick="doSomething" ... />
<asp:Button runat="server" onClick="doSomething" ... />
Behind the scenes, ScriptOnly is a very simple control...
public class ScriptOnly : Control
protected override void Render(HtmlTextWriter writer)
//Render contents to a StringWriter
StringWriter renderedContents = new StringWriter();
//write out the contents, line by line
StringReader sr = new StringReader(renderedContents.ToString());
while (sr.Peek() >= 0)
// This could be optimized to write on one line; but
// I've found this makes it easier to debug when
// looking at a page's source
private string jsEscapeText(string value)
if (string.IsNullOrEmpty(value)) return value;
// This, too, could be optimzied to replace character
// by character; but this gives you an idea of
// what to escape out
/* \ --> \\ */
/* ' --> \' */
/* " --> \" */
/* (newline) --> \n */
/* (creturn) --> \r */
/* </script> string */
When "pre-reistered" in your web.config, it works just as well as the NOSCRIPT tag.