How to Make a HTML Table by C# code in asp.net programming

How to Make a HTML Table by C# code in asp.net programming :



As we know that we can make Html table in Asp.net by C# code ; so first we need to use 
using System.Text;
Then use 

StringBuilder sb = new StringBuilder();
Means make the object of StringBuilder Class ;
the use Append() function and all table's html code in this string .
then print this string in a control. as like we use literal control,

now here we give a Example :


 Code in .aspx.cs file :

StringBuilder sb = new StringBuilder();
        sb.Append("<table border='.5' style='border-style: groove;width:100%'>");
        sb.Append("<tr>");
        sb.Append("<td style='width:50px;color: #FFFFFF' bgcolor='#333333'>col1: ID </td>");
        sb.Append("<td style='width:50px;color: #FFFFFF' bgcolor='#333333'>col 2 :Name</td>");  
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.Append("<td style='width:50px;color: #FFFFFF' bgcolor='#333333'>col1: ID </td>");
        sb.Append("<td style='width:50px;color: #FFFFFF' bgcolor='#333333'>col 2 :Name</td>");           sb.Append("</tr>") 
        sb.Append("<tr>");
        sb.Append("<td style='width:50px;>col1: ID </td>");
        sb.Append("<td style='width:50px>col 2 :Name</td>");         
        sb.Append("</tr>") ;
        sb.Append("<tr>");
        sb.Append("<td style='width:50px;>col1: ID </td>");
        sb.Append("<td style='width:50px>col 2 :Name</td>"); 
        sb.Append("</tr>") ;
   sb.Append("</table>") 
  Literal1.Text = sb.ToString();

Code in .aspx page :

<table style="width: 100%">
        <tr>
            <td align="center" colspan="2">
                <asp:Literal ID="Literal1" runat="server"></asp:Literal>
            </td>
        </tr>
</table>

Result:



Col 1 :ID Col
Col 2: Name









                                          (  HTML table By C# Code )


Other Asp.net related post:

Comments

  1. it might help to others
    http://www.code-sample.com/2014/09/angularjs-documentation.html

    ReplyDelete
  2. Hi,
    Thanks for sharing this codding. This same codding I have got from Hostbuddy.com recently. But your post is easier than him. So I am able to understand this codding part is an easy way.

    ReplyDelete

Post a Comment