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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...