Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add row on each time when button of USA table Clicked. it adds only one row but not more.

same if i click one time on button in UK table it creates new row in USA table not in UK table. `
can someone help me solve it.

Defaul.aspx Code
ASP.NET
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">


    <asp:Table BorderWidth="1px" ID="myTable" runat="server" Width="100%">
    <asp:TableRow BorderWidth="3px">
        <asp:TableCell> Table One for USA </asp:TableCell>


    </asp:TableRow>
</asp:Table>


<br />

<asp:Button ID="Button988" runat="server" Text="Button"
           onclick="Button988_Click" />

           <br />


           <asp:Table BorderWidth="1px" ID="myTable2" runat="server" Width="100%">
    <asp:TableRow BorderWidth="3px">
        <asp:TableCell>Table Two for UK </asp:TableCell>


    </asp:TableRow>
</asp:Table>


<asp:Button ID="Button1" runat="server" Text="Button"
           onclick="Button988_Click" />
</asp:Content>


Default.aspx.cs Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    TextBox tb1 = new TextBox();
    TextBox tb2 = new TextBox();
    Button b1 = new Button();

    TextBox tb3 = new TextBox();
    TextBox tb4 = new TextBox();
    Button b2 = new Button();



    protected void Page_Load(object sender, EventArgs e)
    {

        b1.Text = "-";
        b1.Click += new EventHandler(b1_Click);


    }
    protected void Button988_Click(object sender, EventArgs e)
    {

            TableRow tRow = new TableRow();

            myTable.Rows.Add(tRow);

            TableCell tCell1 = new TableCell();
            TableCell tCell2 = new TableCell();
            TableCell tCell3 = new TableCell();
            tRow.Cells.Add(tCell1);
            tRow.Cells.Add(tCell2);
            tRow.Cells.Add(tCell3);

            tCell1.Controls.Add(tb1);
            tCell1.Controls.Add(tb2);
            tCell1.Controls.Add(b1);
            tRow.Visible = true;
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        TableRow tRow = new TableRow();

        myTable2.Rows.Add(tRow);

        TableCell tCell4 = new TableCell();
        TableCell tCell5 = new TableCell();
        TableCell tCell6 = new TableCell();
        tRow.Cells.Add(tCell4);
        tRow.Cells.Add(tCell5);
        tRow.Cells.Add(tCell6);

        tCell4.Controls.Add(tb3);
        tCell5.Controls.Add(tb4);
        tCell6.Controls.Add(b2);
        tRow.Visible = true;
    }

    void b1_Click(object sender, EventArgs e)
    {

        tb1.Dispose();
        tb2.Dispose();

    }
}
Posted
Updated 26-Mar-15 12:27pm
v2
Comments
sudevsu 26-Mar-15 16:32pm    
Did it throw an error while debugging?
Member 11336034 27-Mar-15 3:22am    
no its does'nt through dubugging error.
ramyajaya 26-Mar-15 19:41pm    
Problem 2 is because of this
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button988_Click" />

<asp:Button ID="Button988" runat="server" Text="Button"
onclick="Button988_Click" />

Both the onclick event refers to the same server side function which adds a row in myTable which is your USA table
Member 11336034 27-Mar-15 3:21am    
Thanks sir ramyajaya,

I have changed the onclick function name. Now its adding textBoxes to the relevant table.

Step 1-When is that when i click the USA table it creates in USA table.

Step 2-Next i want to create for the UK table and i click the UK table button it creates for the UK table but unfortunately hides the textBoxes of USA table which created before clicking the UK table. will you help please.

Code is here


Default.aspx


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">


<asp:Table BorderWidth="1px" ID="myTable" runat="server" Width="100%">
<asp:TableRow BorderWidth="3px">
<asp:TableCell> Table One for USA






<br />

<asp:Button ID="Button988" runat="server" Text="Button"
onclick="Button988_Click" />

<br />


<asp:Table BorderWidth="1px" ID="myTable2" runat="server" Width="100%">
<asp:TableRow BorderWidth="3px">
<asp:TableCell>Table Two for UK






<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />










Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
Button b1 = new Button();

TextBox tb3 = new TextBox();
TextBox tb4 = new TextBox();
Button b2 = new Button();




protected void Page_Load(object sender, EventArgs e)
{

b1.Text = "-";
b1.Click += new EventHandler(b1_Click);
b2.Text = "-";
b2.Click += new EventHandler(b2_Click);


}
protected void Button988_Click(object sender, EventArgs e)
{

TableRow tRow = new TableRow();

myTable.Rows.Add(tRow);

TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);

tCell1.Controls.Add(tb1);
tCell1.Controls.Add(tb2);
tCell1.Controls.Add(b1);
tRow.Visible = true;
}


protected void Button1_Click(object sender, EventArgs e)
{

TableRow tRow = new TableRow();

myTable2.Rows.Add(tRow);

TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
TableCell tCell6 = new TableCell();
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);

tCell4.Controls.Add(tb3);
tCell5.Controls.Add(tb4);
tCell6.Controls.Add(b2);
tRow.Visible = true;
}



void b1_Click(object sender, EventArgs e)
{

tb1.Dispose();
tb2.Dispose();
b1.Dispose();

}

void b2_Click(object sender, EventArgs e)
{

tb3.Dispose();
tb4.Dispose();
b2.Dispose();

}

}
ramyajaya 27-Mar-15 4:28am    
Please check the below mentioned comments for your problem 1 hope that may be the reason for this .


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