2012-12-07 34 views
2

我是ASP.NET新手。我從這個教程http://www.asp.net/web-forms/tutorials/security/roles/assigning-roles-to-users-cs創建用戶嚮導 - 空引用異常未被用戶代碼處理

下面的第4步採取createUserWiazrd名爲RegisterUserWithRoles的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。 你能幫我理解這個錯誤的起源嗎?

在此先感謝!

+0

什麼是異常的堆棧跟蹤? –

回答

0

雖然我不使用所提供的嚮導此功能,以及堆棧跟蹤將是有益的,我有理由相信這個問題是這裏 -

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

的FindControl已ID您選擇SpecifyRolesStep不會在你的ASPX匹配ID文件 -

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

的「爲」關鍵字(而不是簡單地使用語法(的WizardStep)RegisterWithRoles.FindControl(「特異性鑄造yRolesStep「))應該意味着即使FindControl沒有返回一個對象,或者該對象是一個不兼容的類型,SpecifyRolesStep也會被安全地設置爲null,所以最好在可能的情況下使用這個」as「關鍵字這裏)和然後檢查使用以下syntax-

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

當然是並不總是能夠繼續運行程序流,其中對象爲空操縱得到的對象之前空值,它可能是關鍵功能,因此您可能無法處理該錯誤,而不是拋出帶有更具體錯誤消息的ApplicationException來幫助調試。

在這種情況下,無論如何,當你打算做的所有事情都拋出異常時,你顯然必須作爲開發人員花時間檢查空值(以及混亂你的代碼)。

拋開背景,您需要更改您用於SpecifyRolesStep的FindControl參數,或者重命名ASPX中的WizardStep「SpecifyRoles」以匹配。

相關問題