Monday, September 27, 2010

How to dynamically add a row of controls based on TextBox value : Asp.net - Part 3

Demo:

<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <script runat="server">
        public int NumberOfRows
        {
            get { return int.Parse(this.Request.Form[this.txtRows.UniqueID] ?? "1"); }
            set { this.txtRows.Text = value.ToString(); }
        }
        public int NumberOfColums
        {
            get { return int.Parse(this.Request.Form[this.txtColmns.UniqueID] ?? "4"); }
            set { this.txtColmns.Text = value.ToString(); }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Table table = new Table();
            for (int i = 0; i < this.NumberOfRows; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < this.NumberOfColums; j++)
                {
                    TableCell td = new TableCell();
                    td.Controls.Add(new TextBox() { ID = "Cell" + i + j });
                    tr.Controls.Add(td);
                }
                table.Rows.Add(tr);
            }
            this.phCells.Controls.Add(table);
        }                   
    </script>
</head>
<body>
 <form id="form1" runat="server">
        Rows: <asp:TextBox runat="server" ID="txtRows" Text="1" />
        - Coloums: <asp:TextBox runat="server" ID="txtColmns" Text="4" />
        <hr />
        <asp:PlaceHolder runat="server" ID="phCells"></asp:PlaceHolder>
        <asp:Button runat="server" ID="btnCreate" Text="Create" />  
 </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...