【问题标题】:Strange Behaviour System.NullReferenceException on crosspage postback with Master Page带有母版页的跨页回发的奇怪行为 System.NullReferenceException
【发布时间】:2012-06-29 00:50:18
【问题描述】:

我正在使用 C# ASP.NET,我进行了跨页回发,它工作正常,没有母版页。

但是在使用 Master page 时,相同的逻辑失败并得到上述错误。我是 ASP.NET 新手,请详细告诉我。

我的代码是

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="View_Information.aspx.cs" Inherits="View_Information" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
    Module 3: Assignment 1</p>
<div>
        Total Data You Have Entered
        <br />
        <br />
        Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />
        Address:&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server"></asp:Label>
        <br />
        <br />
        Thanks for submitting your data.<br />
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Placehodler2" Runat="Server">
</asp:Content>

后面的代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class View_Information : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (PreviousPage != null && PreviousPage.IsPostBack)
    {
        TextBox nametextpb = (TextBox)PreviousPage.FindControl("TextBox1");
        //Name of controls should be good to identify in case application is big
        TextBox addresspb = (TextBox)PreviousPage.FindControl("TextBox2");
        Label1.Text =  nametextpb.Text; //exception were thrown here

        Label2.Text =  addresspb.Text;

    }

    else
    {
        Response.Redirect("Personal_Information.aspx");
    }
}
}

【问题讨论】:

  • 哪里抛出异常?
  • 第17行,Label1.Text = nametextpb.Text;
  • 不,同样的错误,奇怪但相同的逻辑在没有母版页的情况下工作,这两个文本框没有在母版页中定义,但我也尝试了 Page.Master.FindControl()

标签: c# exception-handling nullreferenceexception


【解决方案1】:

问题在于,对于母版页,您的控件现在需要放在ContentPlaceHolder 控件中。

FindControl 方法可用于访问 ID 不是 在设计时可用。该方法只搜索页面的 直接或顶级容器;它不会递归搜索 页面上包含的命名容器中的控件。访问 子命名容器中的控件,调用 FindControl 该容器的方法。

您现在需要递归搜索控件以从PreviousPage 中找到您的TextBox 控件。你可以看到an example of that here。在该站点上还指出,您可以通过其完整的UniqueID 获得控制权,在您的情况下,它将通过以下方式工作:

TextBox nametextpb = (TextBox)PreviousPage.FindControl("ctl00$ContentPlaceHolder1$TextBox1")

编辑:认为包含我用来定位目标控件的UniqueID 的代码不会有什么坏处。

在 Page_Load 中:

var ids = new List<string>();
BuildControlIDListRecursive(PreviousPage.Controls, ids);

以及方法定义:

private void BuildControlIDListRecursive(ControlCollection controls, List<string> ids)
{
    foreach (Control c in controls)
    {
        ids.Add(string.Format("{0} : {2}", c.ID, c.UniqueID));
        BuildControlIDListRecursive(c.Controls, ids);
    }
}

然后从 ids 列表中找到您的控件。

【讨论】:

  • ContentPlaceHolder ph1 = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");并且比应用相同的逻辑并且它起作用了。
【解决方案2】:

(TextBox)PreviousPage.FindControl("TextBox1");一定是返回了null,表示没有找到控件。

尝试改用Page.Master.FindControl()

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多