Tuesday, January 11, 2011

C#: Serialize an ASP WebControl

In one of my other posts I offered a solution to be able to serialize an object (pretty much any object) into string (http://kodesharp.blogspot.com/2009/10/c-serialize-object-to-xml-string.html). However that method does not work for web controls as there are more to the object like viewstate etc. So this method needs to be used instead, for being able to serialize WebControls:

public static string GetSerializedXmlStringForWebControls(WebControl webControl)
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter tw = new HtmlTextWriter(new StringWriter(sb));
webControl.RenderControl(tw);
return tw.InnerWriter.ToString();
}

No comments:

Post a Comment

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.