Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

c# - ASP.net .FindControl() and GridView returning null

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...