Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
On grid RowDataBound based on some condition i've added imagebutton dynamically. parallelly i want to add event for this button click. here is my code snippet
protected void GV3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell cell = new TableCell();
                ImageButton bttn = new ImageButton();
                bttn.CommandName = "Delete";
                bttn.ImageUrl = "delete.png";
                bttn.ToolTip = "Click to delete this record";
                bttn.CommandArgument = e.Row.Cells[0].Text;
                cell.Controls.Add(bttn);
                e.Row.Cells.Add(cell);
                bttn.OnClientClick = "return confirm('Are you sure you to delete " + e.Row.Cells[0].Text + "?');";
}
}

That message popup box comes, but i'm unable to catch that click event. while click on a row i want to delete that row, so want to create event dynamically. for that i've used this following dynamic click event

C#
bttn.Click += new ImageClickEventHandler(b_Click);

private void b_Click(object sender, EventArgs e)
    {
//delete record
}

but this above code also not working.

Please help me to solve this. Thanks in advance.
Posted

1 solution

Do like below...
C#
ImageButton bttn = new ImageButton();
bttn.CommandName = "Delete";
bttn.ImageUrl = "delete.png";
bttn.ToolTip = "Click to delete this record";
bttn.CommandArgument = e.Row.Cells[0].Text;

bttn.OnClientClick = "return confirm('Are you sure you to delete " + e.Row.Cells[0].Text + "?');";
bttn.Click += new ImageClickEventHandler(b_Click);

cell.Controls.Add(bttn);
e.Row.Cells.Add(cell);

Now, event would look like...
C#
void b_Click(object sender, ImageClickEventArgs e)
{
    // Do whatever you want to do.
}
 
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