0

我正在使用 AJAX 控件工具包来创建Tabpanels. 根据以下代码,每个面板都填充了一个网格视图。

现在,我想每行添加一个按钮。单击它时,它应该作为参数传递该行的单元格之一,但是由于 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);
}
4

2 回答 2

0

您可以将选择按钮添加到网格中,如下所示:

Gridview1.AutoGenerateSelectButton=true;
于 2012-11-09T13:31:05.307 回答
0

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

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

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

希望这对某人有帮助!

于 2012-11-14T13:43:22.833 回答