尝试对!IsPostBack进行FindControl的NullReferenceException错误 [英] NullReferenceException error trying to FindControl on !IsPostBack

查看:99
本文介绍了尝试对!IsPostBack进行FindControl的NullReferenceException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FormView中有一个Menu/MultiView控件设置,我试图将第一个菜单项设置为Page_Load上选择的状态.我在尝试设置Selected = True的行上收到NullReferenceException错误.

I have a Menu/MultiView control setup inside a FormView and I am trying to set the first menu item as selected on Page_Load. I'm getting a NullReferenceException error on the line where I'm trying to set Selected = True.

标记:

<asp:FormView ID="FormView1" runat="server" CellPadding="4" DataKeyNames="ProjectID" DataSourceID="ProjectDetailsSQL" ForeColor="#333333">
    <ItemTemplate>
        <h1><asp:Label ID="Label1" runat="server" Text='<%# Eval("ProjectID") %>' /> - <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProjectName") %>' /></h1>
        <asp:Menu ID="mnuProject" runat="server" CssClass="MenuStyle" Orientation="Horizontal" OnMenuItemClick="mnuProject_MenuItemClick" EnableViewState="false">
            <staticselectedstyle backcolor="Gray" borderstyle="Solid" bordercolor="Black" borderwidth="1"/>
            <Items>
                <asp:MenuItem Text="General" Value="0" />
                <asp:MenuItem Text="Scope" Value="1" />
                <asp:MenuItem Text="CAD" Value="2" />
                <asp:MenuItem Text="PM" Value="3" />  
                <asp:MenuItem Text="Submittals" Value="4" />
                <asp:MenuItem Text="ChangeOrders" Value="5" />
                <asp:MenuItem Text="Timecards" Value="6" />
                <asp:MenuItem Text="Docs" Value="7" />
                <asp:MenuItem Text="Log" Value="8" />
                <asp:MenuItem Text="Financials" Value="9" />
            </Items>
        </asp:Menu>
        <asp:MultiView ID=MultiView1></asp:MultiView>
    </ItemTemplate>
</asp:FormView>

CodeBehind:

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Menu mnuProject = (Menu)FormView1.FindControl("mnuProject");
                mnuProject.Items[0].Selected = true; <----- Exception thrown here
            }
        }

我也尝试过 Menu mnuProject =(Menu)FormView1.Row.FindControl("mnuProject"); ,而mnuProject仍然返回null.我只能猜测我没有为FindControl提供正确的位置.任何纠正我的语法的帮助将不胜感激.

I have also tried Menu mnuProject = (Menu)FormView1.Row.FindControl("mnuProject"); and mnuProject is still coming back as null. I can only guess that I'm not giving it the right location for FindControl. Any help correcting my syntax would be greatly appreciated.

推荐答案

findControl方法不是递归的.

The findControl method is NOT recursive.

这意味着它将尝试在您请求的项目中找到您的控制对象,而不是其子对象.换句话说,它正在FormView1中寻找mnuProject,而不是FormView1的任何子控件.

That means it will attempt to find your contol in the item you request, but NOT its children. In other words, it is looking for mnuProject within FormView1 but NOT any of FormView1's child controls.

这是我用来解决该问题的通用实现.您需要使用递归来实现您想要的方式...幸运的是,我已经打开了这个项目:-)

This is a generic implementation I used to resolve it. You need to use recursion for this to behave the way you want... lucky for you I had this project open :-)

public static class pageHelpers
{
            public static System.Web.UI.Control FindControlRecursive(System.Web.UI.Control root, string id)
            {
                if (root.ID == id)
                {
                    return root;
                }

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

让我们重构您的页面代码,以便我们确定是否可以解决此问题.

And let's refactor your page code, so we can determine if that resolved this issue.

  Menu mnuProject = (Menu)pageHelpers.FindControlRecursive(FormView1,"mnuProject");
  // lets test to see if our FindControlRecursive method worked before doing anything else
  if(mnuProject == null) {throw new Exception("FindControlRecursive failed!");}

  mnuProject.Items[0].Selected = true; <----- Exception thrown here

这篇关于尝试对!IsPostBack进行FindControl的NullReferenceException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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