Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a dynamic table using c#. How do I set a colspan on the dynamic table using a textbox?

For example if I place a value 3 in the text box and click on Apply Span button then the colspan of a dynamic table should change accordingly.

I am new to c#, pls help

Thanks.

What I have tried:

<
C#
    public partial class WebForm1 : System.Web.UI.Page
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void CreateRuntime_Table()
    {
        int tblRows = int.Parse(txtrow.Text);
        int tblCols = int.Parse(txtcol.Text);

        Table tbl = new Table();
        tbl.BorderWidth = 3;
        tbl.BorderStyle = BorderStyle.Solid;
        tbl.ID = "myTable";

        for (int i = 1; i <= tblRows; i++)
        {

            TableRow tr = new TableRow();
            for (int j = 1; j <= tblCols; j++)
            {
                TableCell tc = new TableCell();
                TextBox txtbox = new TextBox();
                txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
                //Add the control to the table cell
                tc.Controls.Add(txtbox);
                tr.Controls.Add(tc);
            }

            tbl.Rows.Add(tr);
        }

        form1.Controls.Add(tbl);
    }

    protected void Unnamed_Click(object sender, EventArgs e)
    {
        CreateRuntime_Table();
    }

   
Html Here:
ASP.NET
<pre>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server"> 
    <title></title>
    </head>
    <body>
    <link href="StyleSheet1.css" rel="stylesheet" />
    <form id="form1" runat="server">
    <div>
     <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <table>
            <caption>Creating a dyanamic Table</caption>
            <tr>
                <td>
                    Row:       <asp:TextBox ID="txtrow" 
    placeholder="No of Rows Here" runat="server" AutoCompleteType="Disabled" > 
    </asp:TextBox>
                    <br />
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    
                    Coloum: <asp:TextBox ID="txtcol" placeholder="No of Coloums Here" 
      runat="server" AutoCompleteType="Disabled"></asp:TextBox>
                    <br /> <br />
                </td>
           </tr>
            <tr>
                <td>
                     <asp:Button Text="Create Table" runat="server" CssClass="button" 
       OnClick="Unnamed_Click"/>
                </td>
            </tr>
        </table>
        <br />

            <h4>Appying Row/Col span on Dynamic Tabes</h4>
            Colspan:   <asp:TextBox ID="txtcolspan" placeholder="Colspan 
     Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
            <span></span>
            <asp:Button Text="Apply Span" runat="server" CssClass="button" 
    OnClick="Unnamed2_Click"/>
            <br />
            <br />
            Rowspan: <asp:TextBox ID="txtrowspan" placeholder="Rowspan Here" 
    runat="server" AutoCompleteType="Disabled"></asp:TextBox>
            <span></span>
            <asp:Button Text="Apply Span" runat="server" CssClass="button" 
    OnClick="Unnamed3_Click"/>       
         </div>
    </form>
   </body>
   </html>
Posted
Updated 29-Aug-18 12:01pm
v2
Comments
ZurdoDev 29-Aug-18 8:22am    
You'll just have to add that to your code. I'm not sure what you want from us.

1 solution

The TableCell has a ColumnSpan and RowSpan attributes that you can set.

Just set those properties when regenerating your table on postbacks.
 
Share this answer
 
Comments
Syf AK 30-Aug-18 3:06am    
Sorry for my English, i already tried this but the table which i had created its not coming across the postbacks.

This is what i had tried:

int colspan = int.Parse(txtcolspan.Text);
Table table = Session["table"] as Table;
for (int i = 1; i <= colspan; i++)
{
TableCell TC = new TableCell();
TC.ColumnSpan = colspan;
}
Vincent Maverick Durano 30-Aug-18 9:06am    
Since you are creating it dynamically, you have to recreate your table on each and every postbacks so your changes will be reflected.
Vincent Maverick Durano 30-Aug-18 9:48am    
Also, see your other thread and look for the solution that i've provided: https://www.codeproject.com/Questions/1258309/How-can-store-and-retrieve-a-dynamic-table-in-sess
Syf AK 31-Aug-18 3:14am    
Thanks for your fast reply. Iam not trying to create a dynamic table instead i am trying to set colspan and rowspan on a dynamic table. please refer this link for clear idea: https://www.codeproject.com/Questions/1258205/How-can-I-create-col-span-or-row-span-on-dynamic-t
Vincent Maverick Durano 31-Aug-18 10:02am    
based on your code: Table tbl = new Table();; you are creating a dynamic table. And in-order for you to reflect the colspan you assigned on postbacks, you have to recreate the table and apply the changes on the table cells. Otherwise, you would need to set the colspan at the client using JavaScript/jQuery which I don't know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900