将click事件添加到gridview中的动态生成按钮

将click事件添加到gridview中的动态生成按钮

本文介绍了将click事件添加到gridview中的动态生成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向gridview onRowDataBound事件添加按钮



I am adding buttons to my gridview onRowDataBound Event

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");
        }





但点击按钮后,btn_Check_Click事件永远不会被调用。



如何制作按钮。点击调用btn_Check_Click?



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

how to make the button.Click call btn_Check_Click?

推荐答案

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




--Amit


这篇关于将click事件添加到gridview中的动态生成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:50