簡體   English   中英

如何在動態創建的Gridview中創建按鈕?

[英]How to create button in dynamically created Gridview?

我正在使用AJAX Control Toolkit創建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;

我剛剛找到了一個可能對此感興趣的解決方案:

首先,您應該在databind之前加入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);
        }
    }

希望這對某人有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM