2012-04-11 20 views
1

我在嘗試從引用的程序集使用ASPX頁時遇到此問題。該頁面由內容頁面及其母版頁組成。只有當從其他Web項目訪問任何內容頁面Web控件時纔會引發異常,但是當從同一個項目訪問該頁面時不會發生這種情況。NullReferenceException在通過程序集引用的內容頁上引發異常

起初,這個頁面應該是普通的ASPX頁面,然後他們工作得很好(即,這個異常沒有發生),但是我們的上級決定將它們包裝到MasterPages中以獲得一些可重用性或者什麼這有點奇怪,因爲這個ASPX頁面是自動生成的)。

所以,我們得到了這個麻煩了:/

編輯: 我加入一些代碼來幫你幫我:)

母版頁:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" 
Inherits="WebApplicationTemplate.MasterPage" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <asp:ContentPlaceHolder ID="headPlaceHolder" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 
<body> 
    <form id="form" runat="server"> 
     <asp:ContentPlaceHolder ID="formPlaceHolder" runat="server"> 
     </asp:ContentPlaceHolder> 
    </form> 
</body> 
</html> 

內容Page:

<%@ MasterType VirtualPath="~/MasterPage.Master" %> 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" 
    CodeBehind="TestPage.aspx.cs" Inherits="WebApplicationTemplate.TestPage" %> 

<asp:Content ID="headContent" ContentPlaceHolderID="headPlaceHolder" runat="server"> 
</asp:Content> 
<asp:Content ID="formContent" ContentPlaceHolderID="formPlaceHolder" runat="server"> 
    <asp:TextBox ID="id1" runat="server" Text="Text" MaxLength="40" Style="top: 100; 
     left: 100; width: 100; height: 100; position: absolute;" /> 
</asp:Content> 

在內容頁面代碼中引發異常的函數後面:

public void Foo() 
{ 
id1.Text = "something"; //Object reference not set to an instance of an object. 
} 

正如我之前說過的,當我通過引用程序集從另一個項目訪問此頁面時,我只有這個問題。 我不知道是否必須在任何web.config中配置某些內容,無論是在主頁面項目還是引用前項目的程序集的項目。

+3

顯示一些代碼。 – 2012-04-11 07:20:22

回答

0

問題已解決。

在內容頁中的第一行把事情搞糟了,所以這是現在的樣子:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" 
    CodeBehind="TestPage.aspx.cs" Inherits="WebApplicationTemplate.TestPage" %> 
<asp:Content ID="headContent" ContentPlaceHolderID="headPlaceHolder" runat="server"> 
</asp:Content> 
<asp:Content ID="formContent" ContentPlaceHolderID="formPlaceHolder" runat="server"> 
    <asp:TextBox ID="id1" runat="server" Text="Text" MaxLength="40" Style="top: 100; 
     left: 100; width: 100; height: 100; position: absolute;" /> 
</asp:Content> 

因此,移除<%@ MasterType VirtualPath="~/MasterPage.Master" %>線之後,問題解決了,但現在我們要投的頁面.Master屬性如果想從ContentPage訪問其字段,屬性,控件等等,通過刪除前面提到的行,我們不知道我們引用了哪個MasterPage類。

事情是這樣的:

MasterPage MP; 

    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     //Setting the master page 
     MP = ((WebApplicationTemplate.MasterPage)Master); 
    } 

    protected void Foo() 
    { 
     //Accessing a MasterPage control 
     MP.Bar.Visible = false; 
    } 

很好,這是它。無法猜測這是如何解決我的問題,但它確實如此。如果有人能夠闡明這一點並滿足我的好奇心,我會很高興。 這是非常不可能的,但希望任何人發現這個有用的,如果有人碰巧遇到這個問題。

0

這種錯誤通常發生在您缺少存在於您正在訪問的程序集的其他解決方案中的一些配置信息時。

您需要在當前項目中從您訪問其他程序集的位置複製這些設置(例如,app.config或web.config)。

希望這會有所幫助。

+0

哇,現在我想知道爲什麼我沒有想到過!謝謝,馬上就試試! – 2012-04-11 07:23:31