2

I am looking for a way to convert WebForm controls to text.

Is it possible to do this:

TextBox tx = new TextBox();
tx.Text = "test";

string html = tx.HTML();

Where html would be:

<input type="text" value="test"/>
Petras
  • 4,686
  • 14
  • 57
  • 89

2 Answers2

8
 var sb = new StringBuilder();
 var htw = new HtmlTextWriter(new System.IO.StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture));

 var tx = new TextBox {Text = "test"};
 tx.RenderControl(htw);
 var html = sb.ToString();
 Response.Write(html);
lak-b
  • 2,115
  • 4
  • 18
  • 30
2

You can use RenderControl() method http://msdn.microsoft.com/en-us/library/htwek607(v=vs.80).aspx and get the string in Stream object.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124