"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 System.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:Usersdas.arunabhDocumentsVisual Studio 2008ProjectsESERVICE_SOLUTION-MAINeserviceeserviceManageRoles.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


    该错误表示您正试图给Msg.Text一个值,但Msg.Text不存在于您当前的作用域中。

    链接地址: http://www.djcxy.com/p/27982.html

    上一篇: 元素null错误

    下一篇: “使用新关键字创建对象”实例错误