how to dynamically add the delete and update buttons in every row of my asp.net web table which is generated at run time

i am trying to learn asp.net at my own so i have already learned how to insert data in sql server and how to fetch it from the database and display it in table dynamically but the thing i am trying to do is that I want 2 buttons of delete and update in every row which is linked to the same row only and if i click delete button of row 1 it will delete that row entirely from the web and sql server also. here is the code i am using to fetch data from sql-server and displaying it in the table on button click...

protected void DisplayData_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connStr);
            con.Open();
            SqlCommand cd = new SqlCommand("select * from signup",con);
            SqlDataReader rd = cd.ExecuteReader();
            if(rd.HasRows)
            {
                while(rd.Read())
                {
                    TableRow row = new TableRow();
                    TableCell cell1 = new TableCell(); 
                    cell1.Text = rd[0].ToString();
                    TableCell cell2 = new TableCell(); 
                    cell2.Text = rd[1].ToString();
                    TableCell cell3 = new TableCell(); 
                    cell3.Text = rd[2].ToString();
                    TableCell cell4 = new TableCell(); 
                    cell4.Text = rd[3].ToString();
                    TableCell cell5 = new TableCell(); cell5.Text = rd[4].ToString();
                    TableCell cell6 = new TableCell(); cell6.Text = rd[5].ToString();
                    row.Cells.Add(cell1); 
                    row.Cells.Add(cell2); 
                    row.Cells.Add(cell3); 
                    row.Cells.Add(cell4); 
                    row.Cells.Add(cell5); 
                    row.Cells.Add(cell6);
                    Table1.Rows.Add(row);
                }
            }

        }

Upvotes: 0

Views: 1436

Answers (2)

iamdlm
iamdlm

Reputation: 1973

You and add buttons with:

TableCell btnCell= new TableCell();
Button btn = new Button();
btn.Id = "Id";
btn.Text = "Something";
btn.Click += new EventHandler(BtnHandler_Click);
btnCell.Controls.Add(btn);

row.Cells.Add(btnCell);

The event

protected void BtnHandler_Click(object sender, EventArgs e)
{ 
    // Do something
}

Note: Have a look at GridView, which seems like a better alternative for what you want to achieve.

Upvotes: 1

Hussain Md
Hussain Md

Reputation: 129

Try below code

//Update Button
TableCell cell7 = new TableCell();
Button btn1 = new Button();
btn.Id="btnUpdate";
btn.Text = "Update";
btn.Click += new EventHandler(btnUpdate_Click);
cell7.Controls.Add(btn1);

//Delete Button
TableCell cell8 = new TableCell();
Button btn2 = new Button();
btn.Id="btnDelete";
btn.Text = "Delete";
btn.Click += new EventHandler(btnDelete_Click);
cell8.Controls.Add(btn2);

// add above two cell objects to row object
 row.Cells.Add(cell7);
 row.Cells.Add(cell8); 

//Update button event handler
protected void btnUpdate_Click(object sender,EventArgs e)
{
    // update the record
}

//Delete button event handler
protected void btnDelete_Click(object sender,EventArgs e)
{
    // Delete the record
}

Upvotes: 0

Related Questions