locked
Add table row and cell by clicking on a button RRS feed

  • Question

  • Hi,

    I have an asp:Table and I need when a button is clicked one row and two cells to be added automatically to that table. But it must keep the previously added rows and cells .

        <asp:Button ID="btnAdd" runat="server" Text="Add new" onclick="btnAdd_Click" />
        <asp:Table ID="tbl" runat="server">
        </asp:Table>
    So when the user clicks on btnAdd it adds a row with two cells to table tbl.


            TableRow tRow = new TableRow();
            TextBox txt1 = new TextBox();
            TextBox txt2 = new TextBox();
            TableCell tCell1 = new TableCell();
            tCell1.Controls.Add(txt1);
            TableCell tCell2 = new TableCell();
            tCell2.Controls.Add(txt2);
            tRow.Cells.Add(tCell1);
            tRow.Cells.Add(tCell2);
            tbl.Rows.Add(tRow);
    But when the button is clicked again the previously added rows and cells must be saved. It also should save the values inside the textboxes.
    In my code only the new rows are kept and the old ones are deleted.
    • Moved by OmegaMan Friday, March 5, 2010 4:12 PM (From:Visual C# General)
    Tuesday, February 9, 2010 10:22 AM

Answers

  • hi,

    try this,

    ----------UI Design-------------
     <div>
        <table width="50%" id="tbl" runat="server">
        </table>
       
         <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
     </div>

    -------Code Behind------------

    public partial class TestDynamicTable : System.Web.UI.Page
    {
        ArrayList arrList;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["arrList"] != null)
            {
                arrList = (ArrayList)Session["arrList"];
                for (int i = 0; i < arrList.Count; i++)
                {
                    tbl.Rows.Add((HtmlTableRow)arrList[i]);
                }
                
                //tbl.Rows.Clear();            
            }
            else
            {
                arrList = new ArrayList();
                Session["arrList"] = arrList;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                TextBox tb = tbl.Rows[i].Cells[0].Controls[0] as TextBox;
                arrList[i] = tbl.Rows[i];
            }
            HtmlTableRow tRow = new HtmlTableRow();
            TextBox txt1 = new TextBox(); 
            TextBox txt2 = new TextBox(); 
            HtmlTableCell tCell1 = new HtmlTableCell();
            tCell1.Controls.Add(txt1);
            HtmlTableCell tCell2 = new HtmlTableCell();
            tCell2.Controls.Add(txt2);
            tRow.Cells.Add(tCell1);
            tRow.Cells.Add(tCell2);
            arrList.Add(tRow);
            tbl.Rows.Add(tRow);           
            Session["arrList"] = arrList;        
        }
    }

    Nagarjuna Dilip
    Tuesday, February 9, 2010 11:56 AM
  • hi,

    if any small modifications required........plz alter it if possible.........else post back ur question..............

    public partial class TestDynamicTable : System.Web.UI.Page
    {
        ArrayList arrList;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["arrList"] != null)
            {
                arrList = (ArrayList)Session["arrList"];
                for (int i = 0; i < arrList.Count; i++)
                {
                    tbl.Rows.Add((HtmlTableRow)arrList[i]);
                    LinkButton lnk = tbl.Rows[i].Cells[2].Controls[0] as LinkButton;
                    lnk.Click += new EventHandler(lnk_Click);
                    lnk.ToolTip = i.ToString();
                }
                
                //tbl.Rows.Clear();            
            }
            else
            {
                arrList = new ArrayList();
                Session["arrList"] = arrList;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tbl.Rows.Count; i++)
            {            
                arrList[i] = tbl.Rows[i];            
            }
    
            HtmlTableRow tRow = new HtmlTableRow();
            TextBox txt1 = new TextBox(); 
            TextBox txt2 = new TextBox();
            LinkButton lnk = new LinkButton();
            lnk.Click +=new EventHandler(lnk_Click);
            lnk.Text = "Delete";
            lnk.ToolTip = arrList.Count.ToString();
    
            HtmlTableCell tCell1 = new HtmlTableCell();
            tCell1.Controls.Add(txt1);
            HtmlTableCell tCell2 = new HtmlTableCell();
            tCell2.Controls.Add(txt2);
            HtmlTableCell tCell3 = new HtmlTableCell();
            tCell3.Controls.Add(lnk); 
            
            tRow.Cells.Add(tCell1);
            tRow.Cells.Add(tCell2);
            tRow.Controls.Add(tCell3);
    
            arrList.Add(tRow);
            tbl.Rows.Add(tRow);           
            Session["arrList"] = arrList;        
        }
    
        void lnk_Click(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            int delInd = int.Parse(lnk.ToolTip);
            arrList.RemoveAt(delInd);
            tbl.Rows.RemoveAt(delInd); 
            Session["arrList"] = arrList;   
        }
    }
    

    Nagarjuna Dilip
    Tuesday, February 9, 2010 1:08 PM
  • hi,

    their is a small modification jst change it in the link button event...........to get more efficient output........


    void lnk_Click(object sender, EventArgs e)
        {
            LinkButton lnk1 = (LinkButton)sender;
            int delInd = int.Parse(lnk1.ToolTip);
            arrList.RemoveAt(delInd);
            tbl.Rows.RemoveAt(delInd);
            for (int i = 0; i < arrList.Count; i++)
            {
                //tbl.Rows.Add((HtmlTableRow)arrList[i]);
                LinkButton lnk = tbl.Rows[i].Cells[2].Controls[0] as LinkButton;
                lnk.Click += new EventHandler(lnk_Click);
                lnk.ToolTip = i.ToString();
            }
            Session["arrList"] = arrList;   
        }

    Nagarjuna Dilip
    Tuesday, February 9, 2010 1:49 PM

All replies

  • Hi,

     Please post your asp.net related questions in ASP.Forums in future.

     You have to dynamically add  controls  in Page_Init or Page_Load events at run time so that ViewState will be maintained between postback. Page_Load is preferable to add controls at run time.

    If you add controls in button's click event, the dynamically added controls will not be retained between postbacks.



    Tuesday, February 9, 2010 11:23 AM
  • hi,

    try this,

    ----------UI Design-------------
     <div>
        <table width="50%" id="tbl" runat="server">
        </table>
       
         <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
     </div>

    -------Code Behind------------

    public partial class TestDynamicTable : System.Web.UI.Page
    {
        ArrayList arrList;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["arrList"] != null)
            {
                arrList = (ArrayList)Session["arrList"];
                for (int i = 0; i < arrList.Count; i++)
                {
                    tbl.Rows.Add((HtmlTableRow)arrList[i]);
                }
                
                //tbl.Rows.Clear();            
            }
            else
            {
                arrList = new ArrayList();
                Session["arrList"] = arrList;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                TextBox tb = tbl.Rows[i].Cells[0].Controls[0] as TextBox;
                arrList[i] = tbl.Rows[i];
            }
            HtmlTableRow tRow = new HtmlTableRow();
            TextBox txt1 = new TextBox(); 
            TextBox txt2 = new TextBox(); 
            HtmlTableCell tCell1 = new HtmlTableCell();
            tCell1.Controls.Add(txt1);
            HtmlTableCell tCell2 = new HtmlTableCell();
            tCell2.Controls.Add(txt2);
            tRow.Cells.Add(tCell1);
            tRow.Cells.Add(tCell2);
            arrList.Add(tRow);
            tbl.Rows.Add(tRow);           
            Session["arrList"] = arrList;        
        }
    }

    Nagarjuna Dilip
    Tuesday, February 9, 2010 11:56 AM
  • Suppose that I add a linkbutton and by clicking on it I want to delete the whole row, and all controls on it. How can it be done?
    Tuesday, February 9, 2010 12:42 PM
  • hi,

    if any small modifications required........plz alter it if possible.........else post back ur question..............

    public partial class TestDynamicTable : System.Web.UI.Page
    {
        ArrayList arrList;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["arrList"] != null)
            {
                arrList = (ArrayList)Session["arrList"];
                for (int i = 0; i < arrList.Count; i++)
                {
                    tbl.Rows.Add((HtmlTableRow)arrList[i]);
                    LinkButton lnk = tbl.Rows[i].Cells[2].Controls[0] as LinkButton;
                    lnk.Click += new EventHandler(lnk_Click);
                    lnk.ToolTip = i.ToString();
                }
                
                //tbl.Rows.Clear();            
            }
            else
            {
                arrList = new ArrayList();
                Session["arrList"] = arrList;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tbl.Rows.Count; i++)
            {            
                arrList[i] = tbl.Rows[i];            
            }
    
            HtmlTableRow tRow = new HtmlTableRow();
            TextBox txt1 = new TextBox(); 
            TextBox txt2 = new TextBox();
            LinkButton lnk = new LinkButton();
            lnk.Click +=new EventHandler(lnk_Click);
            lnk.Text = "Delete";
            lnk.ToolTip = arrList.Count.ToString();
    
            HtmlTableCell tCell1 = new HtmlTableCell();
            tCell1.Controls.Add(txt1);
            HtmlTableCell tCell2 = new HtmlTableCell();
            tCell2.Controls.Add(txt2);
            HtmlTableCell tCell3 = new HtmlTableCell();
            tCell3.Controls.Add(lnk); 
            
            tRow.Cells.Add(tCell1);
            tRow.Cells.Add(tCell2);
            tRow.Controls.Add(tCell3);
    
            arrList.Add(tRow);
            tbl.Rows.Add(tRow);           
            Session["arrList"] = arrList;        
        }
    
        void lnk_Click(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            int delInd = int.Parse(lnk.ToolTip);
            arrList.RemoveAt(delInd);
            tbl.Rows.RemoveAt(delInd); 
            Session["arrList"] = arrList;   
        }
    }
    

    Nagarjuna Dilip
    Tuesday, February 9, 2010 1:08 PM
  • hi,

    their is a small modification jst change it in the link button event...........to get more efficient output........


    void lnk_Click(object sender, EventArgs e)
        {
            LinkButton lnk1 = (LinkButton)sender;
            int delInd = int.Parse(lnk1.ToolTip);
            arrList.RemoveAt(delInd);
            tbl.Rows.RemoveAt(delInd);
            for (int i = 0; i < arrList.Count; i++)
            {
                //tbl.Rows.Add((HtmlTableRow)arrList[i]);
                LinkButton lnk = tbl.Rows[i].Cells[2].Controls[0] as LinkButton;
                lnk.Click += new EventHandler(lnk_Click);
                lnk.ToolTip = i.ToString();
            }
            Session["arrList"] = arrList;   
        }

    Nagarjuna Dilip
    Tuesday, February 9, 2010 1:49 PM
  • By the way, for ASP.NET issues, you'd better to post in the ASP.NET official forum,
    http://forums.asp.net/


    Regards,
    Ji Zhou
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, February 10, 2010 8:19 AM
  • It is OK, but when I refresh the page it adds the whole table again, so everything is repeated.
    Wednesday, February 24, 2010 8:25 AM