1

Would someone give a code example how to create and insert HTML at run time?

The HTML is like this:

 <div class="row">
       <div class="col-md-3">
            <img src="example.png" class="profileImage" /> <br />
            <span class="name">Name</span>
            <div class="ver"></div>
            <img class="flag ver" src="star.png" />
            <div class="horizontalBar"></div>
        </div>
    </div>

The close I get was:

 public partial class MyPage : System.Web.UI.Page
    {
     protected void Page_Load(object sender, EventArgs e)
            {
                this.Header.DataBind();
                ContentPlaceHolder contents = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
                Panel row = new Panel() { CssClass = "row" };
                Panel col = new Panel() { CssClass = "col-md-3" };
                Image profile = new Image()
                {
                    CssClass = "profileImage",
                    ImageUrl = "example.jpg"
                };

                row.Controls.Add(col);
                col.Controls.Add(profile);
                contents.Controls.Add(row);
            }
}

It doesn't work (see below error) and isn't full code, for example, what class is equivalent to generate <span>?

I get this error:

The Controls collection cannot be modified because the control contains code blocks

What's the reason of that error? which are those code blocks and how do I fix this?

1 Answer 1

1

I've tested the code here and it's working.

The control equivalent to span is Label, but I think there must be better ways of doing this.

If you really need to dynamically insert HTML code, you can inject it using the LiteralControl, like this:

var html = new LiteralControl(@"<div class=""row"">
                                  <div class=""col-md-3"">
                                    <img src=""example.png"" class=""profileImage"" />
                                    <br />
                                    <span class=""name"">Name</span>
                                    <div class=""ver""></div>
                                    <img class=""flag ver"" src=""star.png"" />
                                    <div class=""horizontalBar""></div>
                                  </div>
                               </div>");
contents.Controls.Add(html);
Sign up to request clarification or add additional context in comments.

4 Comments

How is your asp:content defined? like this <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
I found the source of the error, I've already a HTML tag inside <asp:Content>. THis cause the error. I just added another div inside <asp:Content> and use Controls property from this div
And what's the equivalent of <br>?
I don't think there's a direct equivalent control for <br>, but you could use LiteralControl("<br />"). You can use HtmlGenericControl for other tags, but not <br>, since this class does not support self-closing tags: stackoverflow.com/questions/13493586/…

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.