动态添加链接按钮到ASP网格视图

时间:2018-01-10 23:16:30

标签: c# asp.net gridview webforms

我试图在我的C#语法中使用下面的代码向asp:gridview添加一个链接按钮。输出窗口显示添加的文本标签,因此代码似乎应该迭代。但是,当页面加载时,没有链接按钮。

我该如何设置它以便加载链接按钮?

    protected void gvRR_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    string[] rrar= new string[] { "Value Basis For Customer Support:" };
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (TableCell row in e.Row.Cells)
        {
            if (rrar.Any(x => x == e.Row.Cells[1].Text))
            {
                LinkButton lb = new LinkButton();
                lb.ID = "lbcoded";
                lb.Text = e.Row.Cells[1].Text;
                e.Row.Cells[1].Controls.Add(lb);
                System.Diagnostics.Debug.Print("Label Added");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你会尝试在之前的ItemTemplate中添加LinkBut​​ton而不是动态添加,这会让你更轻松。 在RowDataBound中查找LinkBut​​ton并根据条件显示/ side。

或者,您可以尝试在gvRR_RowCreated事件中添加LinkBut​​ton吗?

protected void gvRR_RowCreated(object sender, GridViewRowEventArgs e)
{
   //Put your code of add link buttone
}