Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created one table with multiple controls and can generate rows with controls on add button click so, now how to 1).add update panel and 2).fire button click event which is creating in rows and in that have to 3).delete current row,and how to 4).add 'submit' button in footer and insert that records into database?. following is my code.

/Generating table with controls/

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GenerateTable(numOfRows);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["RowsCount"] != null)
        {
            numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
            GenerateTable(numOfRows);
        }


    }
private void GenerateTable(int rowsCount)
            {


            Table table = new Table();

            table.ID = "Table1";
            Page.Form.Controls.Add(table);
            const int colsCount = 4;
            //TableFooterRow footer = new TableFooterRow();

            //Button btn = new Button();

            //btn.Width = 120;

            //btn.Text = "Submit Records";
            //btn.Click += new EventHandler(this.OnTestButtonClick);
            //cell.Controls.Add(btn);

            TableHeaderRow header = new TableHeaderRow();
            TableHeaderCell headerTableCell1 = new TableHeaderCell();
            headerTableCell1.Text = "Item Name";
            header.Cells.Add(headerTableCell1);

            TableHeaderCell headerTableCell2 = new TableHeaderCell();
            headerTableCell2.Text = "Unit";
            header.Cells.Add(headerTableCell2);

            TableHeaderCell headerTableCell3 = new TableHeaderCell();
            headerTableCell3.Text = "Quantity";
            header.Cells.Add(headerTableCell3);

            TableHeaderCell headerTableCell4 = new TableHeaderCell();
            headerTableCell4.Text = "Rate";
            header.Cells.Add(headerTableCell4);

            TableHeaderCell headerTableCell5 = new TableHeaderCell();
            headerTableCell5.Text = "Total";
            header.Cells.Add(headerTableCell5);

            table.Rows.Add(header);


            connection.Open();
            command = new SqlCommand();
            command.CommandText = "dbo.sp_Get_Emp";
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = connection;
            command.Parameters.AddWithValue("@registrationId","1014");
            da = new SqlDataAdapter(command);
            ds = new DataSet();
            DataSet dsSecond = new DataSet();
            da.Fill(ds, "FirstTab");
            da.Fill(ds, "SecondTab");

            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();

                for (int k = colsCount-1;k < colsCount; k++)
                {
                    TableCell cell = new TableCell();                  
                    DropDownList dr = new DropDownList();

                    dr.ID = "DropDownList_" + i + "Col_" +k;
                    dr.DataSource = ds.Tables[0];
                    dr.DataTextField = "EmpName";
                    dr.DataValueField = "EmpId";
                    dr.DataBind();
                    cell.Controls.Add(dr);
                    row.Cells.Add(cell);
                }


                for (int j = 0; j <= colsCount-1; j++)
                {
                    TableCell cell = new TableCell();
                    TextBox tb = new TextBox();
                    tb.ID = "TextBoxRow_" + i + "Col_" + j;
                    cell.Controls.Add(tb);                                      
                    row.Cells.Add(cell);
                }

                for (int l = colsCount+1; l < colsCount+2; l++)
                {
                    UpdatePanel uptab = new UpdatePanel();

                    TableCell cell = new TableCell();
                    //cell.Controls.Add(this.BuildTestButton()); 
                    Button button = new Button();

                    button.Width = 120;

                    button.Text = "Delete Row";
                    button.ID = "button" + i+"col"+l;
                    button.Click += new EventHandler(this.OnTestButtonClick);
                    uptab.ContentTemplateContainer.Controls.Add(button);
                    cell.Controls.Add(button);
                    row.Cells.Add(cell);
                }
                table.Rows.Add(row);
                //footer.Cells.Add(cell);
            }
            SetPreviousData(rowsCount, colsCount);
            rowsCount++;
            ViewState["RowsCount"] = rowsCount;
        }


/setting the previous data/

private void SetPreviousData(int rowsCount, int colsCount) {

        Table table = (Table)Page.FindControl("Table1");

        if (table != null)
        {
            for (int i = 0; i < rowsCount; i++)
            {                       
                for (int j = 1; j <= colsCount; j++)
                {
                    TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + (j-1));
                                tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + (j-1)];

                }

            }
        }
    }


/click event of creating buttons/

protected virtual void OnTestButtonClick(object sender, EventArgs e) { //Here want to delete current row with controls. }


/inserting data into database in submitbutton click but cant insert whole records,just inserting first row values./

C#
private void InsertData(int rowsCount, int colsCount) { //string[] text=new string[3]; List text = new List(); Table table = (Table)Page.FindControl("Table1"); if (connection.State == ConnectionState.Closed)

        command = new SqlCommand();
    command.CommandText = "Insert_Item";
    command.CommandType = CommandType.StoredProcedure;
    if (table != null)
    {
        for (int i = 0; i < rowsCount; i++)
        {
            for (int j = 0; j < colsCount; j++)
            {
                TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
                text[j] = tb.Text;
            }
            connection.Open();
            command.Connection = connection;
            command.Parameters.AddWithValue("@packPeriod", text[0]);
            command.Parameters.AddWithValue("@packName", text[1]);
            double price = Convert.ToDouble(text[2]);
            command.Parameters.AddWithValue("@packPrice", price);
            command.ExecuteNonQuery();
            connection.Close();
            command.Dispose();
        }

    }
}
Posted

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