Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Html from Code Behind in Asp.net

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.

like image 619
Vinay Pratap Singh Bhadauria Avatar asked Dec 31 '13 08:12

Vinay Pratap Singh Bhadauria


People also ask

How can access HTML table in ASP.NET code behind?

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.

How to create HTML Table in code Behind in c#?

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();

How can I add dynamic code in ASP.NET using HTML?

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.

How to create Table in ASP.NET using HTML?

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.


4 Answers

For appending HTML to your panel, add a LiteralControl control to your panel:

string yourHTMLstring = "<div class='Main'>....";
pnlUserdata.Controls.Add(new LiteralControl(yourHTMLstring));
like image 157
Yair Nevet Avatar answered Oct 20 '22 18:10

Yair Nevet


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
like image 26
Hans Kesting Avatar answered Oct 20 '22 17:10

Hans Kesting


  1. Take one local string variable TEMP.
  2. Create same html as you want to display on screen and store it in variable TEMP.
  3. You can take html creation of control in separate function based on requirement.
  4. Place that created html as innerHTML to your panel/div.

That's it...

like image 1
Chirag Avatar answered Oct 20 '22 19:10

Chirag


<div id="Div1" runat="server">

Div1.InnerText = "Text";
like image 1
tubefavorites.com Avatar answered Oct 20 '22 18:10

tubefavorites.com