"Use the new keyword to create an object" instance error
I have a masterpage file which is called Masterpage.master which works fine with all my aspx pages except aspx pages in which I try to instantiate stuff in the Page_Load method in the codebehind file.
The aspx file called ManageRoles.aspx looks like this -
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="ManageRoles.aspx.cs"
Inherits="eservice.ManageRoles"
MasterPageFile="~/MasterPage.Master"
%>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<!-- Start of roles -->
<h3>
Manage Roles</h3>
<asp:Label ID="Msg" ForeColor="maroon" runat="server" />
<br />
<!-- End of roles -->
<asp:LoginView ID="LoginView2" runat="server">
<LoggedInTemplate>
<p id="backtoblog"></p>
<!-- End of roles -->
</LoggedInTemplate>
<AnonymousTemplate>
</AnonymousTemplate>
</asp:LoginView>
</asp:Content>
and the code-behind file called ManageRoles.aspx.cs looks like this -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Syst开发者_运维百科em.Web.UI.WebControls;
using System.Web.Security;
namespace eservice
{
public partial class ManageRoles : System.Web.UI.Page
{
private string[] rolesArray;
private string[] usersInRole;
MembershipUserCollection users;
protected void Page_Load(object sender, EventArgs e)
{
// clear the Msg label on each visit
Msg.Text = string.Empty;
}
}
}
The Masterpage.master file contains a content section like this -
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
This is a web application project and it builds fine but throws a runtime exception when I click on the link for ManageRoles.aspx
I am getting the following exception -
System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." Source="eservice" StackTrace: at eservice.ManageRoles.Page_Load(Object sender, EventArgs e) in C:\Users\das.arunabh\Documents\Visual Studio 2008\Projects\ESERVICE_SOLUTION-MAIN\eservice\eservice\ManageRoles.aspx.cs:line 24 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:
I don't think Msg
is a reserved word, but you might want to try renaming it. Most likely the designer didn't create the member variable properly in your Designer.cs file. Try the following (basically, remove then readd the html for the label to get the designer to create the member variable):
- Cut the html code out of the editor
- Switch to the design view of the page
- Switch to the code behind
- Switch back to the page's designer view
- Switch to source view
- Re-Paste the code
- Switch to the designer
- Go back to your codebehind.
- Save and if you're lucky, the code should work.
Generally, I would refrain from doing authentication services inside of masterpage code files...Use a login.aspx.cs Codefile for your actual authentication service and leave masterpage's to do what masterpages are meant for...Display Presentation and Content.
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %> Forms Authentication, Authorization, and User Accounts
User Account Tutorials TODO: Menu will go here...
Note: http://www.asp.net/security/tutorials/an-overview-of-forms-authentication-cs
the error says you are trying to give a value to Msg.Text
, but Msg.Text
does not exist in your current scope.
精彩评论