Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New class inherits from System.Web.UI.Page error 1

Status
Not open for further replies.

saroyal

Programmer
Jul 3, 2002
30
0
0
GB
I have created a new class that inherits from System.Web.UI.Page and then I can alter the web form code behind file to inherit from this new class instead.

This compiles and runs ok but when I try to open the web form in designer view it fails to open with error "An exception occurred while trying to create an instance of NewClass. The exception was 'Object Reference was not set to an instance of an object'.".

I can first switch the code behind file to inherit from System.Web.UI.Page, then edit the web form in designer, save it and the then switch the code behind back to the new class.

Does anyone know if I can open the web form up in designer without this error or the hassle of editing the code behind first?

Many thanks

Simon
 
I do this in my apps, I use a class that inherits from System.Web.UI.Page I don't have this problem, I can open every page from the design box.

If the Class accessible from the form?

Can you send me the class code and the code behind to see if I can identify the problem? I'm not an expert but sometimes others can see better than us while programming.

David
 
--code behind file--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace newnamespace.folder
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : newnamespace.newpageclass
{
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// load code
}
}
}
-- end file --

-- class file --
using System;

namespace newnamespace
{
/// <summary>
/// Based on System.Web.UI.Page this class has a few extra properties
/// and methods regarding user info.
/// </summary>
///

[Flags] public enum AdminRoles : int
{
NoAdmin = 0,
ReadOnly = 1,
Schedules = 2,
ProcessBills = 4,
}

public class newpageclass: System.Web.UI.Page
{
private string userID;
private int adminRole;

public newpageclass()
{
this.userID = &quot;&quot;;
this.adminRole = 0;

if (User.Identity.Name.Length > 0)
{
// calc userID
}
}

}
}
-- end file --

-- web form code --
<%@ Page language=&quot;c#&quot; Codebehind=&quot;file.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;newnamespace.folder.file&quot; %>
(rest not needed)
-- end file
 
Instead of having on the page
Code:
<%@ Page language=&quot;c#&quot; Codebehind=&quot;file.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;newnamespace.folder.className&quot; %>[code] 
try
[code]public class myPage: newnamespace.folder.className
in the code behind. You migh currently have
Code:
public class myPage: System.Web.UI.Page
 
The problem is that your subclass's constructor is assuming a Http request is going on. Try adding a check in the constructor like this:
Code:
        public newpageclass()
        {
           if(System.Web.HttpContext.Current==null)
               return;
            this.userID = &quot;&quot;;
            this.adminRole = 0;

            if (User.Identity.Name.Length > 0)
            {
                // calc userID
            }
        }

That way, the constructor will short circuit when there is no http request, (as in VS designer) and execute properly.

HTH
 
Many thanks as after I modified it to check the current context it worked.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top