1
votes

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:\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:

3
Msg.Text = string.Empty;Arunabh Das
Is there more code in Page_Load, especially statements using rolesArray?Henk Holterman
The code using rolesArray is commented out so I can fix the issue above.Arunabh Das

3 Answers

2
votes

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):

  1. Cut the html code out of the editor
  2. Switch to the design view of the page
  3. Switch to the code behind
  4. Switch back to the page's designer view
  5. Switch to source view
  6. Re-Paste the code
  7. Switch to the designer
  8. Go back to your codebehind.
  9. Save and if you're lucky, the code should work.
0
votes

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

Note: http://www.asp.net/security/tutorials/an-overview-of-forms-authentication-cs

0
votes

the error says you are trying to give a value to Msg.Text, but Msg.Text does not exist in your current scope.