Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在动态创建的Gridview中创建按钮?_C#_Button_Gridview_Datatable - Fatal编程技术网

C# 如何在动态创建的Gridview中创建按钮?

C# 如何在动态创建的Gridview中创建按钮?,c#,button,gridview,datatable,C#,Button,Gridview,Datatable,我正在使用AJAX控件工具包创建Tabpanels。每个面板都按照下面的代码填充一个gridview 现在,我想每行添加一个按钮。单击它时,它应该作为该行的一个单元格的参数传递,但由于Gridview是动态创建的,我不知道如何传递。有什么建议吗 foreach (DataTable dt in DataSet1.Tables) { GridView gv = new GridView(); var thepanel = new AjaxControlToolkit.TabPane

我正在使用AJAX控件工具包创建
Tabpanels
。每个面板都按照下面的代码填充一个gridview

现在,我想每行添加一个按钮。单击它时,它应该作为该行的一个单元格的参数传递,但由于Gridview是动态创建的,我不知道如何传递。有什么建议吗

foreach (DataTable dt in DataSet1.Tables)
{
    GridView gv = new GridView();
    var thepanel = new AjaxControlToolkit.TabPanel();
    gv.DataSource = dt;
    gv.DataBind();
    thepanel.Controls.Add(gv);
    TabContainer.Controls.Add(thepanel);
}

您可以按如下方式向网格中添加选择按钮:

Gridview1.AutoGenerateSelectButton=true;

我刚刚找到了一个对此感兴趣的解决方案:

首先,您应该在数据绑定之前包含fllwg行:

gv.RowDataBound += gv_RowDataBound;
gv.RowCommand += gv_RowCommand;
然后定义RowDataBound以插入Linkbutton:

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

            LinkButton butIgnorar = new LinkButton()
            {
               CommandName = "Ignorar",
               ID = "butIgnorar",
               Text = "Ignorar",
               //optional: passes contents of cell 1 as parameter
               CommandArgument =  e.Row.Cells[1].Text.ToString()
            };
            //Optional: to include some javascript cofirmation on the action
            butIgnorar.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to ignore?');");
            TableCell tc = new TableCell();
            tc.Controls.Add(butIgnorar);
            e.Row.Cells.Add(tc);
        }
    }
最后,从RowCommand调用该命令

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        string currentCommand = e.CommandName;
        string parameter= e.CommandArgument.ToString();

        if (currentCommand.Equals("Ignorar"))
        {
            yourMethodName(parameter);
        }
    }
希望这对某人有帮助

试试这个: