Create dynamic Table based on Row and Column value from TextBox using C# and VB.Net in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I want to create a dynamic table by adding height,width and row colums values by textbox. so, please help me.

     <form id="form1" runat="server">
         Height:<div><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
         Width:<div><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
         Row:<div><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></div>   
         Column:<div><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></div>
        <div style="margin-top:10px">
                    <table class="auto-style1" border="1"">
             <tr>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
             </tr>
             <tr>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
             </tr>
             <tr>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
                 <td>&nbsp;</td>
             </tr>
         </table>
        </div>    
    </form>

 

Posted 4 months ago

Hi nid@patel,

Refer below sample.

HTML

Height:<div>
    <asp:TextBox ID="TextBox1" runat="server" Text="200px"></asp:TextBox></div>
Width:<div>
    <asp:TextBox ID="TextBox2" runat="server" Text="150px"></asp:TextBox></div>
Row:<div>
    <asp:TextBox ID="TextBox3" runat="server" Text="5"></asp:TextBox></div>
Column:<div>
    <asp:TextBox ID="TextBox4" runat="server" Text="5"></asp:TextBox></div>
<br />
<div id="TableHtml" runat="server">
</div>

Namespaces

C#

using System.Text;

VB.Net

Imports System.Text

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table border='1' height = '" + TextBox1.Text + "' width='" + TextBox2.Text + "'>");
        sb.Append("<tr>");
        for (int i = 0; i < Convert.ToInt16(TextBox4.Text); i++)
        {
            sb.Append("<th>" + i + "</th>");
        }
        sb.Append("</tr>");
        for (int j = 1; j <= Convert.ToInt16(TextBox3.Text); j++)
        {
            sb.Append("<tr>");
            for (int k = 0; k < Convert.ToInt16(TextBox4.Text); k++)
            {
                sb.Append("<td>" + j + "</td>");
            }
            sb.Append("</tr>");
        }
        sb.Append("</tr></table>");
        TableHtml.InnerHtml = sb.ToString();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
    If Not Me.IsPostBack Then
        Dim sb As StringBuilder = New StringBuilder()
        sb.Append("<table border='1' height = '" & TextBox1.Text & "' width='" + TextBox2.Text & "'>")
        sb.Append("<tr>")
        For i As Integer = 0 To Convert.ToInt16(TextBox4.Text) - 1
            sb.Append("<th>" & i & "</th>")
        Next
        sb.Append("</tr>")
        For j As Integer = 1 To Convert.ToInt16(TextBox3.Text)
            sb.Append("<tr>")
            For k As Integer = 0 To Convert.ToInt16(TextBox4.Text) - 1
                sb.Append("<td>" & j & "</td>")
            Next
            sb.Append("</tr>")
        Next
        sb.Append("</tr></table>")
        TableHtml.InnerHtml = sb.ToString()
    End If
End Sub

Screenshot