I want to add HTML structure and control like this from code behind into a panel
<div class='Main'>
<div class='cst'>
First Name
</div>
<div class='csc'>
<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>
</div>
<div class='cst'>
First Name
</div>
<div class='csc'>
<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>
</div> <div class='cst'>
First Name
</div>
<div class='csc'>
<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>
</div>
<div class='end'>
</div>
</div>
<asp:Panel runat="server" CssClass="sxpnl" ID="pnlUserdata">
</asp:Panel>
If i try to add like this
HtmlGenericControl divcontrol = new HtmlGenericControl();
divcontrol.Attributes["class"] = "sxro sx1co";
divcontrol.TagName = "div";
pnlUserSearch.Controls.Add(divcontrol);
Label question = new Label();
questionDescription.Text = "text";
pnlUserSearch.Controls.Add(question);
It will add controls one after another, how can i make controls go nested like that as shown above.
Try HtmlTable Class. You have to add runat="server" tag to the HTML table and access that table by Id in Code behind. and then CAST it to HTMLTable.
You will need to import the following namespaces. Inside the Page Load event, first a dynamic DataTable is created with some dummy data. Then using the StringBuilder class, an HTML String of HTML Table is built and is later assigned to the Literal control's Text property. DataTable dt = new DataTable();
You can set the InnerHtml attribute of just about any control. Whenyou need to have dynamic HTML, better to have a div in aspx page with an id and runat="server" then edit the HTML as You need in code-behind.
A table Web control creates an HTML table in simple HTML with the help of the <tr> and <td> tags. You can use the <table>,<tr>, and <td> tags to create a table and its rows in HTML. For example, the HTML code in Listing 7-25 creates a table with two rows and three columns with their values.
For appending HTML to your panel, add a LiteralControl
control to your panel:
string yourHTMLstring = "<div class='Main'>....";
pnlUserdata.Controls.Add(new LiteralControl(yourHTMLstring));
Don't add that child control to the panel, add it to the control that should be the parent:
HtmlGenericControl divcontrol = new HtmlGenericControl();
divcontrol.Attributes["class"] = "sxro sx1co";
divcontrol.TagName = "div";
pnlUserSearch.Controls.Add(divcontrol);
Label question = new Label();
question.Text = "text";
divcontrol.Controls.Add(question); // add to the new div, not to the panel
That's it...
<div id="Div1" runat="server">
Div1.InnerText = "Text";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With