Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

My asp table has two cells. One is textbox, another one is an image (+ or -).
When I click '+', I want a new row to be added with a textbox and an image '-'.
While clicking on that '-' Image, the corresponding row will be deleted.

A new row is being added, when I click '+' on first time. But when I click '+' on second time, it 's replacing the existing row.

My .cs code:
C#
TableRow row = new TableRow();
protected void CreateTextBox(object sender, ImageClickEventArgs e)
        {
            TextBox MyTextBox = new TextBox();
            //Assigning the textbox ID name 
            ImageButton img = sender as ImageButton;
            TableCell cell = new TableCell();
            TableCell cell1 = new TableCell();
            ImageButton minus = new ImageButton();

            cell.ID = "tblCell" + ++system;
            cell1.ID = "tblCell1" + system;
            row.ID = "tblRow" + system;

            minus.ID = "imgSystem" + system;
            minus.ImageUrl = "~/Images/minus.png";

            MyTextBox.ID = "txtSystem" + system;
            MyTextBox.Width = 142;
            MyTextBox.Height = 17;
            MyTextBox.TextMode = TextBoxMode.SingleLine;

            cell.Controls.Add(MyTextBox);
            cell1.Controls.Add(minus);

            row.Cells.Add(cell);
            row.Cells.Add(cell1);
            tbl.Rows.Add(row);
            minus.Click += new System.Web.UI.ImageClickEventHandler(deleteRow);
       }
     protected void deleteRow(object sender, System.Web.UI.ImageClickEventArgs e)
      {
            TableRow row = new TableRow();
            ImageButton img = sender as ImageButton;
            string val = img.ID;
            string id = val.Substring(val.Length - 1, 1);
            if (val.Contains("System"))
            {
                TableRow rw = (TableRow)row.FindControl("tblRow" + id);
                rw.Visible = false;
            }
      }


My aspx code:
C#
<asp:Table ID="tbl" runat="server">
        <asp:TableRow runat="server">
           <asp:TableCell runat="server">
                 <asp:TextBox ID="txtSystem1" runat="server"></asp:TextBox>
           </asp:TableCell>
        <asp:TableCell runat="server">
            <asp:ImageButton ID="imgSystem" runat="server" AlternateText="System"      ImageUrl="~/Images/plus1.png" OnClick="CreateTextBox" />
        </asp:TableCell>
        </asp:TableRow>
</asp:Table>


Please help. Thanks in advance
Posted
Updated 18-Mar-14 23:08pm
v3

create new instance of row inside CreateTextBox event
C#
protected void CreateTextBox(object sender, ImageClickEventArgs e)
        {
            ...
            TableRow row = new TableRow(); 
            row.Cells.Add(cell);
            row.Cells.Add(cell1);
            tbl.Rows.Add(row);
            minus.Click += new System.Web.UI.ImageClickEventHandler(deleteRow);
       }

Happy Coding!
:)
 
Share this answer
 
Comments
srmohanr 19-Mar-14 2:37am    
Hi, Thanks for your help.
But still it's replacing the existing row.
srmohanr 19-Mar-14 5:09am    
Some one, pls help me
Aarti Meswania 19-Mar-14 7:35am    
when you click button page is getting refresh and asp table control is reset that's why it happen see solution 2 it is generating table when page is loaded first time
Hi Mohan...
the issue is with the postback...
Have a look at this link.... it clearly explains how to achieve the same
Dynamically Adding Rows in ASP Table on Button Click event
 
Share this answer
 

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