0
private void GetGroupListHtml()
{                
                //...Code omitted
                String htmlCode = String.Empty;

                foreach (DataRow _DataRow in _DataTable.Rows)
                {
                    htmlCode += @"<div class=""box collapsed-box"">
                         <div class=""box-header with-border"">
                             <h3 class=""box-title"">
                                  <asp:Label ID=""groupTitleLabel"" runat=""server"" Text=" +_DataRow["groupName"] +  @"""></asp:Label>
                             </h3>
                             <div class=""box-tools pull-right"">
                                 <button class=""btn btn-box-tool"" data-widget=""collapse"" data-toggle=""tooltip"" title=""Expand""><i class=""fa fa-plus""></i></button>
                             </div>
                         </div>
                       </div>";
                }
}

I want to add the htmlCodes to a div. Is it possible to do it this way? Basically i have a string variable which appends the html codes. I want to dump this code in between the div tag and return the new webpage to the user.

<div id="groupListDiv" runat="server"></div>

1 Answer 1

1

First of all use StringBuilder for concatenating strings instead of using the += sign.

StringBuilder sb = new StringBuilder();
sb.Append("<...>"); // Your html code

After that if you want to append this generated html to your page, use a <asp:Panel runat="server" id="pnlCode"></asp:Panel> tag that is located on your page where you want to add the html code.

Then in your code behind simply do:

this.pnlCode.Controls.add(new LiteralControl() { Text = sb.ToString() });

This takes your html as a string, put it into a control and then append it to your panel onto your page.

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

4 Comments

there is no problem use div also
Correct, but I prefer using ASP.NET controls
Hi it works. Then how do i code a click event for each of the button in the htmlCode. <button class=""btn btn-box-tool"" ><i class=""fa fa-plus""></i></button>
That you should ask in a new question because its about a new problem.

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.