2

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>"

1 Answer 1

3

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>

Sign up to request clarification or add additional context in comments.

1 Comment

If this answer helps you, click green tick on left side of answer to make question is answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.