Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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;
        }


OUTPUT I GOT :
First Add Click event it added controls displayed after the Footer Row.
if i click on it again it replaces the last row i.e., newly added controls


PLEASE HELP ME AND I NEED TO ADD THE EVENTS TO THE DYNAMICALLY ADDED CONTROLS IN THE NEXT STAGE

IM NOT ABLE TO GIVE THE OUTPUT IMAGE IN THIS

PLEASE HELP ME


DESIGN PAGE

ASP.NET
<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>
Posted
Updated 20-Jul-14 3:45am
v5
Comments
[no name] 20-Jul-14 10:45am    
Do you really think that screaming at people will get them to help you? If you want to add events to controls, then do it. There is nothing at all stopping you.
sandhya.T 2011 20-Jul-14 14:01pm    
Please, I Didn't mean that way...
I'm just giving the idea of my thoughts, that's it.
[no name] 20-Jul-14 19:09pm    
Okay good. As long as you don't actually need help with a problem or have a question then scream all you want.
sandhya.T 2011 24-Jul-14 0:40am    
no sir, I'm working on a small project.... in that the problem was raised otherwise I never waste my time & others (seriously it was true).

1 solution

TblPassenger and TblPassengerRow is dynamic or static?
seems the problem is in taking the count.

please refer these links:

for table creation..

http://geekswithblogs.net/dotNETvinz/archive/2009/06/29/faq-dynamically-adding-rows-in-asp-table-on-button-click.aspx[^]

for dynamic events and others..

http://www.codemag.com/article/0305101[^]
 
Share this answer
 
Comments
sandhya.T 2011 18-Jul-14 2:59am    
TblPassenger table is a static with a HeaderRow, Row with controls and FooterRow contains ADD button

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