I’m having this issue when trying to use an ASPX page from a referenced assembly. This page consists of a content page and its master page. The exception only raises when any of the content page webcontrols are accessed from another web project, but that doesn’t happen when that page is visited from the same project it belongs to.
At first, this pages were supposed to be regular ASPX pages, and then they worked great (ie this exception didn’t happen), but our higher-ups decided to wrap them into MasterPages for some reusability sake or something (which is kinda odd since this ASPX pages are autogenerated).
So, we got into this trouble now : /
EDIT:
I’m adding some code to help you to help me 🙂
Master Page:
<%@ 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>
Content 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>
Function that raises the exception in the content page code behind:
public void Foo()
{
id1.Text = "something"; //Object reference not set to an instance of an object.
}
As I said before, I only got this problem when I access this page from another project through a referenced assembly.
I don’t know if I have to configure something in any web.config, whether in the Master page project or the one which references the assembly of the former project.
Problem solved.
The first line on the Content Page messed it up, so this is how it looks now:
So after removing the
<%@ MasterType VirtualPath="~/MasterPage.Master" %>line, the problem was solved, although now we have to cast the Page.Master property if one wants to access to its fields, properties, controls, etc… from the ContentPage, cause by removing the aforementioned line, we don’t know anymore which MasterPage class are we refering to.Something like this:
And well, this is it. Can’t guess how this solves my problem, but it does. I’ll be very glad if someone could shed some light upon this and satisfy my curiosity.
It’s very unlikely but, hope anyone find this usefull if someone happen to get stuck with this very problem.