2

我是 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. 你能帮我理解这个错误的根源吗?

提前致谢!

4

1 回答 1

0

尽管我不使用提供的向导来实现此功能,并且堆栈跟踪会很有用,但我有理由相信问题出在此处-

WizardStep SpecifyRolesStep = RegisterWithRoles.FindControl("SpecifyRolesStep") as WizardStep;

您选择SpecifyRolesStep的 FindControl ID与您的 ASPX 文件中的 ID 不匹配 -

<asp:WizardStep ID="SpecifyRoles" runat="server" AllowReturn="False" 
            StepType="Step" Title="Specify Roles">

"as" 关键字(而不是简单地使用语法(WizardStep)RegisterWithRoles.FindControl("SpecifyRolesStep")进行转换)应该意味着即使 FindControl 不返回对象或对象是不兼容的类型,因此最好在可能的情况下使用此“as”关键字(如您在此处所使用的那样),然后在使用以下语法操作生成的对象之前检查空值 -

if (RoleList != null)
{ 
  //code working with RoleList object
}
else
{ 
   //handle missing control
}

当然,在对象为空的情况下并不总是可以继续程序流,这可能对功能至关重要,因此除了抛出带有更具体的错误消息以帮助调试的 ApplicationException 之外,您可能无法处理错误。

在这种情况下,当您要做的只是抛出异常时,您显然必须作为开发人员始终花时间检查空值(并使代码混乱)做出判断。

除了背景之外,您要么需要更改用于 SpecifyRolesStep 的 FindControl 参数,要么将 ASPX 中的 WizardStep 重命名为“SpecifyRoles”以匹配。

于 2013-01-13T10:38:48.557 回答