Thursday, 29 May 2014

Adding rows on button click

No comments
.ASPX

<form id="form1" runat="server">
    <div>
       <asp:Panel ID="p1" runat="server">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server">
            
       
        </asp:PlaceHolder>
        </asp:Panel>
        <br />
        <br />
        <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" Text="Add" />
       
    </div>
    </form>

.CS

static int number_rows = 1;
    protected void Page_Init(object sender, EventArgs e)
    {
        ViewState["RowsCount"] = number_rows    
    }
 protected void Page_Load(object sender, EventArgs e)
    {
 if (!IsPostBack)
        {
            generate_table();
        }
        else
        {
              string CtrlID = string.Empty;
         if (Request.Form["__EVENTTARGET"] != null &&
                     Request.Form["__EVENTTARGET"] != string.Empty)
            {
                generate_table();             
            }
            else
            {
                //to check which button or image button click caused the postback
                foreach (string controlID in Request.Form)
                {
                    Control objControl = Page.FindControl(controlID);
                    if (objControl is Button)
                    {
                        CtrlID = objControl.ID;
                        if (CtrlID == "btnadd")
                        {

                            //now the call will go to Button1_Click function
                        }                      
                    }
                }
            }
        }
    }
 protected void generate_table()
    {
        Panel p1 = new Panel();
        Table table = new Table();
        TableRow row,row1;
        TableCell cell,cell1;
        TextBox tb,tb1;
        Label lbl,lbl1;
        table.ID = "Table1";
        p1.ID = "p1";
        int s_rows = Convert.ToInt32(ViewState["RowsCount"].ToString());

        for (int k = 1; k <= s_rows; k++)
        {
            row = new TableRow();
            cell = new TableCell();
            row1= new TableRow();
            cell1 = new TableCell();

            tb = new TextBox();
            tb.ID = "tb_" + k;          
            tb.AutoPostBack = true;

            tb1 = new TextBox();
            tb1.ID = "tb1_" + k;
            tb1.AutoPostBack = true;

            lbl = new Label();
            lbl.ID = "lbl" + k;
            lbl.Text = "name";

            lbl1 = new Label();
            lbl1.ID = "lbl1" + k;
            lbl1.Text = "Address";

            cell.Controls.Add(lbl);
            cell.Controls.Add(tb);

            cell1.Controls.Add(lbl1);
            cell1.Controls.Add(tb1);
            tb.Style.Add(HtmlTextWriterStyle.MarginLeft, "120px");
            tb1.Style.Add(HtmlTextWriterStyle.MarginLeft, "104px");
            row.Cells.Add(cell);
            table.Rows.Add(row);

            row1.Cells.Add(cell1);
            table.Rows.Add(row1);           
        }

        PlaceHolder1.Controls.Add(table);       
    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        int new_rows_count = Convert.ToInt32(ViewState["RowsCount"]) + 1; //add one rows at a time
        ViewState["RowsCount"] = new_rows_count;
        generate_table();
        setdata();
    }
protected void setdata()
    {
        Table table = (Table)Page.FindControl("Table1");
        if (table != null)
        {

            foreach (TableRow tr in table.Rows)
            {
                foreach (TableCell tc in tr.Cells)
                {
                    foreach (Control ct in tc.Controls)
                    {
                        if (ct is TextBox)
                        {
                            ((TextBox)ct).Text = Request.Form[ct.ID];
                        }
                      
                    }
                }
            }
        }
    }

}