如何在gridview RowDataBound c#asp.net中创建按钮单击事件 [英] how to create button click event in gridview RowDataBound c# asp.net

查看:140
本文介绍了如何在gridview RowDataBound c#asp.net中创建按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在gridview c#asp.net中的rowdatabound中创建按钮并单击事件,如下面的代码

i am trying to create button and click event in rowdatabound in gridview c# asp.net like below code

 protected void btnerror_Click(object sender, EventArgs e)
{
         GridView gv = new GridView();
        gv.RowDataBound += gv_RowDataBound;
        gv.RowCommand += gv_RowCommand;
        gv.RowCreated += gv_RowCreated;

        gv.EnableViewState = true;

        gv.DataSource = _dt;
        gv.DataBind();
}


void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        ImageButton btnUpdate = new ImageButton();

        btnUpdate.ID = "btnupdate";
        btnUpdate.ImageUrl = "~/SmartAdmin/Images/update.png";
        btnUpdate.ToolTip = "Click Update";
        btnUpdate.CommandName = "update";


        btnUpdate.Click += btnUpdate_Click;

        TableCell tc = new TableCell();

        tc.Controls.Add(btnUpdate);
        e.Row.Cells.Add(tc);

    }
}


void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "update")
    {

    }
}

单击该按钮的单击事件未触发...

while click that button click event is not firing ...

我犯了错误...

谢谢你.

推荐答案

您真的要手动创建GridView吗?我对此表示强烈怀疑.而是声明性地将其添加到aspx-page并使其在btnerror_Click中可见.

Do you really want to create the GridView manually? I strongly doubt that. Instead add it declaratively to the aspx-page and make it visible in btnerror_Click.

不要动态创建控件并在RowDataBound中注册事件处理程序,而要在RowCreated中注册,该事件处理程序会在每次回发时触发(与RowDataBound相对):

Don't create the control dynamically and register the event handler in RowDataBound but in RowCreated which is triggered on every postback (as opposed to RowDataBound):

void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton btnUpdate = new ImageButton();
        btnUpdate.Click += btnUpdate_Click;
        TableCell tc = new TableCell();

        tc.Controls.Add(btnUpdate);
        e.Row.Cells.Add(tc);
    }
}

void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {    
        ImageButton btnUpdate = (ImageButton)e.Row.FindControls("btnupdate") 
        btnUpdate.ID = "btnupdate";
        btnUpdate.ImageUrl = "~/SmartAdmin/Images/update.png";
        btnUpdate.ToolTip = "Click Update";
        btnUpdate.CommandName = "update";
    }
}

因此,请在RowCreated中创建它,但在RowDataBound中对其进行初始化,在这里您还可以访问数据源(如果需要).

So create it in RowCreated but initialize it in RowDataBound where you can also access the datasource (if required).

还请注意,您不应该在每个回发中仅DataBind GridView if(!IsPostBack)(如果是这种情况).因此,请在分配数据源的位置添加此检查.

Also note that you should DataBind the GridView only if(!IsPostBack) not on every postback (if that's the case). So add this check where you assign the datasource.

这篇关于如何在gridview RowDataBound c#asp.net中创建按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆