0

I have a GridView and dynamically added LinkButton in GridView cell:

protected void TestGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (var l in links)
            {
               e.Row.Cells[6].Controls.Add(l.button);
               PostBackTrigger trigger = new PostBackTrigger();
               trigger.ControlID = l.button.ID;
               UpPanel.Triggers.Add(trigger);
            }
        }
 }

Links are added, its ok, but clicking on link, GridView refreshes and links disappears. When I delete this condition from page_load function, I have a problem with page index changing event - GridView does not refresh:

if (!IsPostBack)
{
    TestGrid.DataBind();
}

What can I do to stop links disappearing and keep page index functional? And is there a better way to add many LinkButtons in one GridView cell? Thank you!

Guilherme Holtz
  • 474
  • 6
  • 19
  • Why not place the buttons in the GridView already? Because if that is not an option, using dynamic controls and enable paging without double hitting the database can become complex. – VDWWD Feb 13 '18 at 15:03
  • Because i don't know how many LinkButtons will be, their count is dinamic too. And this is the big problem. – Natalia Ch Feb 14 '18 at 06:11
  • Then you have to remove the IspostBack check. And then after setting the PageIndex, call DataBind again (double hit). That way it works both with dynamic controls and paging. – VDWWD Feb 14 '18 at 07:31
  • According to this: https://stackoverflow.com/questions/279455/gridview-rowdatabound-doesnt-fire-on-postback the RowDataBound event will only fire during a postback when the grid data has changed. To help suggest a workaround, can you define what action your links should be performing during the postback? – Mark Cooper Feb 14 '18 at 08:50

0 Answers0