1

Now I have done some research on the MSDN on how to get the controls from a Page. http://msdn.microsoft.com/en-us/library/yt340bh4(v=vs.100).aspx

And I have done research on how to get the page reference from a static context. Access current instance of Page from a static class

But when I try to loop through the list of Page controls it comes up null and no gridview.

Now I need that gridview because I am databinding it to a Datatable I have previously filled with a Database call. The static method I am doing this inside of is a WebMethod so it looks like this:

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public static string viewApps(){}

Here is my C# code in my static method

            GridView applications = new GridView();
            Page masspage = HttpContext.Current.CurrentHandler as Page;
            foreach(Control childcontrol in masspage.Controls)
            {
                if(childcontrol is GridView){
                    applications = childcontrol as GridView;
                }
            }

            applications.DataSource = dt;
            applications.DataBind();

I thought it would be simple, but I guess not so much. If there were a way to get a reference to my GridView based on id that would also be useful.

Here is my asp.net code. It is a page that inherits from a master page.

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
        <ContentTemplate>
             <asp:GridView ID="ApplicationsGridView" runat="server"

                      AutoGenerateColumns="True" >

               <%--Styling and column info omitted --%>

                  </asp:GridView>
           </ContentTemplate>
   </asp:UpdatePanel>
Community
  • 1
  • 1
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • 3
    You cannot get it from a webmethod, only in an actual lifecycle of the page. Note that all objects are disposed at the server after the page was sent to the client. – Tim Schmelter Aug 28 '13 at 19:38
  • Well, there is a Gridview in my aspx code file I need to databind it a datatable, How do I do that then from a static context. – Alexander Ryan Baggett Aug 28 '13 at 19:42
  • If you use an `UpdatePanel` anyway, why don't you postback asynchronously? – Tim Schmelter Aug 28 '13 at 19:43
  • You mean using Ajax? Or do you mean like using an asp:button onclick event? I am working on a database-driven website. I originally tried doing everything in update panels. But they got overwhelmed with too many databound controls and became unresponsive, especially to events that effect visibility or appearance of said databound controls. I have about 2-3 stackoverflow posts relating to that. =( – Alexander Ryan Baggett Aug 28 '13 at 19:48
  • So I redid this particular page from the ground up using jQuery's $.ajax(); method and WebMethods in the codebehind. I am trying to do as much as possible using ajax, but a populating a multi-row table is a little tough so I decided to use the gridview with an update panel. – Alexander Ryan Baggett Aug 28 '13 at 19:51
  • Okay. I got it to work using an asp.net button and converting the method to a normal one. Good enough. I will give it to you as an answer if you repost your comment as one. – Alexander Ryan Baggett Aug 28 '13 at 20:13

1 Answers1

0

It's not much right but... Your gridview control is protected. Define a public property GridView MyGrid that return the protected control (just to not edit the designer). Else you need recursion in

foreach(Control childcontrol in masspage.Controls)   
{
    if(childcontrol is GridView)
    {
        applications = childcontrol as GridView;
    }
    else
    {
        // call this function recursively
    }
}
Emanuele
  • 648
  • 12
  • 33