Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am adding buttons to my gridview onRowDataBound Event

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var firstCell = e.Row.Cells[1];
                firstCell.Controls.Clear();
                Button btn_Check = new Button();
                btn_Check.ID = "btn_Check";
                btn_Check.Text = firstCell.Text;
                btn_Check.Click += new EventHandler(btn_Check_Click);
                firstCell.Controls.Add(btn_Check);
            }
        }
        protected void btn_Check_Click(object sender, EventArgs e)
        {
            Response.Write("btn_Check_Click event called");
        }


but on clicking the button the btn_Check_Click event is never called.

how to make the button.Click call btn_Check_Click?
Posted

1 solution

You should add dynamic controls in RowCreated[^] event of the GridView. This will create controls at DataBinding and as well as when rebuilding a page on PostBack.
Try this:
C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var firstCell = e.Row.Cells[1];
        firstCell.Controls.Clear();
        Button btn_Check = new Button();
        btn_Check.ID = "btn_Check";
        btn_Check.Text = firstCell.Text;
        btn_Check.Click += new EventHandler(btn_Check_Click);
        firstCell.Controls.Add(btn_Check);
    }
}

protected void btn_Check_Click(object sender, EventArgs e)
{
    Response.Write("btn_Check_Click event called");
}



--Amit
 
Share this answer
 
v2
Comments
maverick12131 28-Jun-13 6:26am    
the button is not getting added to the grid if I use GridView1_RowCreated instead of GridView1_RowDataBound.
Also In GridView1_RowDataBound button is added to the grid but it dosent call the btn_Check_Click method
_Amy 28-Jun-13 6:29am    
Instead of Button Click Event, try assigning CommandName and handle the command in RowCommand event.
Masoom Mir 28-Jun-13 8:25am    
+5
_Amy 28-Jun-13 13:34pm    
Thank you. :)
maverick12131 1-Jul-13 1:37am    
Now I am doing this.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{ if (e.Row.RowType == DataControlRowType.DataRow)

{ var firstCell = e.Row.Cells[1];

firstCell.Controls.Clear();

Button btn_Check = new Button();

btn_Check.ID = "btn_Check";

btn_Check.Text = firstCell.Text;

btn_Check.CommandArgument = btn_Check.ID;

btn_Check.CommandName = "Check";

firstCell.Controls.Add(btn_Check); } }

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)

{ Response.Write("btn_Check Click event called"); }

But still nothing happens on the click of the button in the grid
Also the GridView1_OnRowCommand is never called on the button click

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