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

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

问题描述

我查看了网站上的页面,但似乎无法找到足以解决我的问题的内容,因此希望有人知道该怎么做.我正在调试其他人编写的一些代码,并且在使用 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 始终为空.我在 LoginView 的面板中有一个声明的 GridView,它基本上设置如下.

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

当我去运行代码时,我在 GridView1 上得到 NullRefrenceException.我是否需要深入面板以引用 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?

更改了我的代码片段以包含匿名模板的信息

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

推荐答案

查找子控件的控件是一个经常出现的问题.您可以考虑使用扩展方法,以便您可以轻松调用 Jeff Atwood 的递归子控件(如 Simon 的回答中所述)......或您编写的任何版本.这只是使用其他帖子中的代码的示例:

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

这是代码.

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天全站免登陆