3

I have looked over the pages on the site, but cant seem to find something general enough for my problem, so was hoping someone knows what to do. I am debugging some code someone else wrote and am having problems with a GridView statement.

My problem is that my gridview is always null. I have a declared GridView in a panel which is in a LoginView, which is basically set up as the following.

<asp:LoginView ID="LoginView1" runat="server" onviewchanged="LoginView1_ViewChanged">
<AnonymousTemplate>&nbsp;Please <a href="../Default.aspx"> Log In </a></AnonymousTemplate>
<LoggedInTemplate>
        <asp:Panel ID="Panel1" runat="server">
            <asp:GridView ID="GridView1" runat="server" 
                AutoGenerateColumns="False" CellPadding="2" 
                DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal" 
                BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
                BorderWidth="1px" Width="970px" OnRowCommand="GridView1_RowCommand" 
                PageSize="40" AllowSorting="True">

After that, in a C# file, I have the following statement

   GridView GridView1 = (GridView)LoginView1.FindControl("GridView1");

When I go to run the code, I get the NullRefrenceException on GridView1. Do I need to dig down into the panel to refrence the GridView, or should I be able to access it from the main LoginView1 segment?

Edit:Changed my code snippet to include the information for the Anonymous Template

user1816892
  • 85
  • 3
  • 8

2 Answers2

2

Finding the controls of a child control is an issue that comes up a lot. You can consider an extension method so you can easily call Jeff Atwood's recursive child control (as referenced in Simon's answer)... or whatever version of it you write. This is just an example using the code from that other post:

GridView GridView1 = (GridView)LoginView1.FindControlRecursive("GridView1");

Here's the code.

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

            foreach (Control c in root.Controls)
            {
                Control t = FindControlRecursive(c, id);
                if (t != null)
                {
                    return t;
                }
            }

            return null;
        } 
    }
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • Alright, this seems to have fixed my problem. Thank you so much – user1816892 Nov 12 '12 at 01:09
  • 1
    Actually, it does not seem to work. I have implemented the code but it seems to always return null, as I still get the null exception problem. – user1816892 Nov 12 '12 at 02:12
  • You never answered my other question. Are you logged in when you are doing it? What's in the anonymous template? If you aren't logged in, then yes it will be null. – MikeSmithDev Nov 12 '12 at 02:14
  • Sorry, I just got around to answering that. The anonymous screen is just a login screen. When I go to debug the page, the gridview is null and the page will not load for debugging, so it does not even give me the chance to log in. – user1816892 Nov 12 '12 at 02:18
  • The logged in template will not be there if you aren't logged in. Paste that code into your anonymous template and you will see it work. It will help if you post your code where it is breaking. – MikeSmithDev Nov 12 '12 at 02:19
  • That makes it compile, is there a way to make it only work for those logged in though? As it currently is, it seems to automatically load the gridviews when you go to the page. – user1816892 Nov 12 '12 at 02:26
  • Yes... don't run your code for GridView1 variable if the person isn't logged in. Or you can just check to see if it the value null. If so, don't do what you are trying to do. – MikeSmithDev Nov 12 '12 at 02:29
  • Ah, I got it now. Thank you for all the help Mike, kinda new to asp.net so Im not the best at it. – user1816892 Nov 12 '12 at 02:35
1

FindControl will only check direct descendants of the control you're using it on. It won't work recursively through the childrens-children.

Jeff Atwood actually blogged about this aaaaggeesss ago:

http://www.codinghorror.com/blog/2005/06/recursive-pagefindcontrol.html

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138