Friday 17 May 2013

Widgets

Create Row & Column in Table Using Button in Asp.net

Demo-:


Code-:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        int rows = Int32.Parse(TextBox1.Text);
        int columns = Int32.Parse(TextBox2.Text);
        for (int row = 0; row < rows; row++ )
        {
            TableRow newRow = new TableRow();
            Table1.Controls.Add(newRow);
            for (int column = 0; column < columns; column++ )
            {
                TableCell newCell = new TableCell();
                newCell.Text = "Cell" + row.ToString();
                newCell.Text += "; Column" + column.ToString();
                newCell.BorderStyle = BorderStyle.Solid;
                newCell.BorderWidth = Unit.Pixel(1);
                newCell.BorderColor = System.Drawing.Color.DodgerBlue;
                newRow.Controls.Add(newCell);
            }
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Create Row & Column in Table Using Button in Asp.net </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr><td colspan=2><h2 style="color:red">Row & Column in Table</h2></td></tr>
        <tr>
        <td>
        <asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="#FF9900" Text="Rows">
        </asp:Label></td>
        <td><asp:TextBox ID="TextBox1" runat="server" BackColor="skyblue" ForeColor="FloralWhite">
        </asp:TextBox></td>
     
        </tr>
        <br /><tr><td> <asp:Label ID="Label2" runat="server" Font-Bold="true" ForeColor="#FF9900" Text="Columns">
        </asp:Label></td>
        <td><asp:TextBox ID="TextBox2" runat="server" BackColor="skyblue" ForeColor="FloralWhite">
        </asp:TextBox>
        </td>
        </tr>
        <br /><br />
        <tr><td colspan=2> <asp:Button ID="Button1" runat="server" Font-Bold="true" ForeColor="red"  OnClick="Button1_Click" Text="Create Table"/></td></tr>
     
        <br /><br />
        </table>
        <asp:Table ID="Table1"  runat="server"  BorderWidth="1"  BorderColor="DodgerBlue">
        </asp:Table>
     
     
    </div>
    </form>
</body>
</html>



0 comments:

Post a Comment