public partial class Table : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BtnAdd_Click(object sender, EventArgs e) { int r_count = TblPassenger.Rows.Count; int c_count = TblPassengerRow.Cells.Count; Label1.Text += "Actual Rows:" + TblPassenger.Rows.Count.ToString(); AddNewRow(r_count,c_count); } private void AddNewRow(int r_count, int c_count) { for (int i = r_count - 1; i < r_count; i++) { TableRow TbRow = new TableRow(); TblPassenger.Rows.Add(TbRow); for (int j = 0; j < c_count; j++) { TableCell TbCell = new TableCell(); if (j == 0) { Label LblSNo = new Label(); LblSNo.ID = "LblSNo" + i + "" + j; TbCell.Controls.Add(LblSNo); } else { TbRow.Cells.Add(TbCell); TextBox Txt = new TextBox(); Txt.ID = "TxtFirstName" + i + "" + j; TbCell.Controls.Add(Txt); } TbRow.Cells.Add(TbCell); } this.TblPassenger.Controls.Add(TbRow); } this.frmTable.Controls.Add(TblPassenger); Label1.Text += "after adding Rows:" + TblPassenger.Rows.Count.ToString(); r_count = 0; c_count = 0; }
<form id="frmTable" runat="server"> <div> <asp:Table ID="TblPassenger" runat="server" Height="120px" Width="596px" HorizontalAlign="Center" BorderWidth="1" CellPadding="1" CellSpacing="1" ViewStateMode="Enabled"> <asp:TableHeaderRow> <asp:TableHeaderCell>S.No.</asp:TableHeaderCell> <asp:TableHeaderCell>Title</asp:TableHeaderCell> <asp:TableHeaderCell>First Name</asp:TableHeaderCell> </asp:TableHeaderRow> <asp:TableRow ID= "TblPassengerRow"> <asp:TableCell>1.</asp:TableCell> <asp:TableCell> <asp:TextBox ID="TxtTitle1" runat="server"></asp:TextBox> </asp:TableCell> <asp:TableCell> <asp:TextBox ID="TxtFirstName1" runat="server"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableFooterRow> <asp:Tablecell > <asp:Button ID="BtnAdd" runat="server" Text="ADD NEW" OnClick="BtnAdd_Click" ViewStateMode="Enabled" /> </asp:Tablecell> </asp:TableFooterRow> </asp:Table> </div> <asp:Label ID="Label1" runat="server" Text="*"></asp:Label> </form>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)