我是 ASP.NET 的新手。我有一个名为 RegisterUserWithRoles 的 createUserWiazrd,取自本教程的第 4 步http://www.asp.net/web-forms/tutorials/security/roles/assigning-roles-to-users-cs
这是aspx文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="CreateUsers.aspx.cs" Inherits="Membership_CreateUser" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<h2>
Create Users</h2>
<p>
<asp:CreateUserWizard ID="RegisterWithRoles" runat="server"
ContinueDestinationPageUrl="~/Default.aspx" LoginCreatedUser="False"
onactivestepchanged="RegisterWithRoles_ActiveStepChanged">
<WizardSteps>
<asp:CreateUserWizardStep runat="server" />
<asp:WizardStep ID="SpecifyRoles" runat="server" AllowReturn="False"
StepType="Step" Title="Specify Roles">
<asp:CheckBoxList ID="RoleList" runat="server">
</asp:CheckBoxList>
</asp:WizardStep>
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
</p>
<p>
</p>
</asp:Content>
和背后的代码:
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;
public partial class Membership_CreateUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Reference the SpecifyRolesStep WizardStep
WizardStep SpecifyRolesStep = RegisterWithRoles.FindControl("SpecifyRolesStep") as
WizardStep;
// Reference the RoleList CheckBoxList
CheckBoxList RoleList = SpecifyRolesStep.FindControl("RoleList") as CheckBoxList;
// Bind the set of roles to RoleList
RoleList.DataSource = Roles.GetAllRoles();
RoleList.DataBind();
}
}
protected void RegisterWithRoles_ActiveStepChanged(object sender, EventArgs e)
{
// Have we JUST reached the Complete step?
if (RegisterWithRoles.ActiveStep.Title == "Complete")
{
// Reference the SpecifyRolesStep WizardStep
WizardStep SpecifyRolesStep = RegisterWithRoles.FindControl("SpecifyRoles") as
WizardStep;
// Reference the RoleList CheckBoxList
CheckBoxList RoleList = SpecifyRolesStep.FindControl("RoleList") as CheckBoxList;
// Add the checked roles to the just-added user
foreach (ListItem li in RoleList.Items)
{
if (li.Selected)
Roles.AddUserToRole(RegisterWithRoles.UserName, li.Text);
}
}
}
}
我不断收到错误
null reference exception was unhandled by user code - Object reference not set to an instance of an object.
有五个角色,我使用ASP.NET Configuration
. 你能帮我理解这个错误的根源吗?
提前致谢!