简体   繁体   中英

Dynamically Add Link Button To ASP Grid View

I am attempting to use the below code in my C# syntax to add a link button to a asp:gridview. The output window shows the text label added, so it seems the code iterated as it should. However, when the page loads, there is no link button.

How should I set this up so that the link button is loaded?

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

You would try to add LinkButton in ItemTemplate earlier instead of adding dynamically which would make you easier life. Find LinkButton in RowDataBound and show/side based on condition.

Alternatively, could you try add LinkButton in gvRR_RowCreated event instead?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM