Hilmi
Hilmi

Reputation: 3441

Asp.net control to HTML

I'm just trying to get the equivalent HTML code that represent a specific control in asp. for example i have the following label in ASP

Label x=new Label();
x.ID="a123";
x.Text="b123";

i just want to find a way to get

"<span id='a123'>b123</span>"

Upvotes: 2

Views: 1657

Answers (1)

arunes
arunes

Reputation: 3524

You can use this method to render controls to html.

public string RenderControl(Control ctrl)
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    ctrl.RenderControl(hw);
    return sb.ToString();
}

And use

Label x = new Label();
x.ID = "a123";
x.Text = "b123";

var html = RenderControl(x);

will give you <span id="a123">b123</span>

Upvotes: 3

Related Questions