ASP.net .FindControl()和GridView返回null [英] ASP.net .FindControl() and GridView returning null

查看:145
本文介绍了ASP.net .FindControl()和GridView返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了看网站上的页面,但不能似乎找到足够一般我的问题,所以希望有人知道该怎么做。我调试了一些code别人写的和我有一个GridView的声明问题。

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.

我的问题是,我的GridView总是空。我有一个中声明的GridView在一个面板,其是在一个LoginView,其基本上设置为以下

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">

之后,在C#中的文件,我有以下语句

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

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

当我去运行code,我得到GridView1的NullRefrenceException。我需要向下挖掘到面板refrence GridView的,或者我应该能够从主LoginView1段访问它?

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?

编辑:改变了我的code段,以包括匿名模板

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

推荐答案

查找子控件的控制是出现了很多的问题。你可以考虑扩展方法让你可以轻松调用杰夫·阿特伍德的递归子控件(如西蒙的回答中引用)......或任何它的版本,你写的。这仅仅是使用code与其他职位的例子:

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");

这里的code。

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

这篇关于ASP.net .FindControl()和GridView返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆