JD ConleyWriting

.net / asp.net / utility

Stringbuildify!

JD Conley
Updated July 22, 2011

A task that I often end up doing when coding an actual web site (i.e. not writing a sample or some such) is adding client script to a page/control in codebehind using the ClientScriptManager. Let's say you've got the following alert script you want to add to the page so you can use it in a control:

function doAlert(){ alert('welcome!');}

Well, there are now a number of ways to get this into your page, but the quickest, in-line way is to use the ClientScriptManager. Like so:

if (!Page.ClientScript.IsClientScriptBlockRegistered(   this.GetType(), "alert")){   System.Text.StringBuilder sb = new System.Text.StringBuilder();   sb.Append("function doAlert() {");   sb.Append("alert ('welcome!'); }");   Page.ClientScript.RegisterClientScriptBlock(       this.GetType(), "alert", sb.ToString(), true);}

Ok, so that's a bit contrived. You're not going to use a StringBuilder for something that simple. But, with a more complicated script you probably would, especially if the script will be different depending on the state of the page.

I had a little Winforms utility sitting around I've been using for a while to automate that process for me, and I decided to put it up on the web. So, here you go: http://jdconley.com/stringbuildify. You feed the engine a multi-line string (like a script) and it gives you back a StringBuilder with everything properly escaped and such. The code's a bit of a mess at this second, that's why I didn't post it. Enjoy!

Written by JD Conley

I build software people want.
Based in South Lake Tahoe.