0

I have nested GridViews on my page (Default) and I need to get the ID of the GridView inside but it's returning null. I have an UpdatePanel outside the nested GridViews. It's using a masterpage.

GridView gv = (GridView)UpdatePanel1.FindControl("GridViewSchedule");
Ram
  • 661
  • 2
  • 12
  • 37
  • are you sure the gridview is at the first level? – Joan Caron Mar 25 '13 at 22:50
  • Take a look at this solution: [How to find outer control from nested inner control][1] [1]: http://stackoverflow.com/questions/1371290/how-to-find-outer-control-from-nested-inner-control – jiiri Mar 25 '13 at 22:52

1 Answers1

0

Here is the helper method to find a control recursively.

public static Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
        return root;

    return root.Controls.Cast<Control>()
        .Select(c => FindControlRecursive(c, id))
        .FirstOrDefault(c => c != null);
}

// Usage
var gridView = FindControlRecursive(UpdatePanel1, "GridViewSchedule");
Win
  • 61,100
  • 13
  • 102
  • 181