2013-07-01 44 views
1

我有一個DropDownList,用於在註冊時選擇角色。問題是,在.CreatedUser事件,我做模板中的DropDownList.SelectedValue爲空

var roleDropDownList = (DropDownList)RegisterUser.CreateUserStep 
.ContentTemplateContainer.FindControl("RoleDropDownList"); 

和後來roleDropDownList.SelectedValue是空""。問題是什麼?

這裏是我的代碼:

<%@ Page Title="Register" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Register.aspx.cs" Inherits="myLeMS.Account.Register" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" OnCreatedUser="RegisterUser_CreatedUser"> 
     <LayoutTemplate> 
      <asp:PlaceHolder ID="wizardStepPlaceholder" runat="server"></asp:PlaceHolder> 
      <asp:PlaceHolder ID="navigationPlaceholder" runat="server"></asp:PlaceHolder> 
     </LayoutTemplate> 
     <WizardSteps> 
      <asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server"> 
       <ContentTemplate> 
        <h2> 
         Create a New Account 
        </h2> 
        <p> 
         Use the form below to create a new account. 
        </p> 
        <p> 
         Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length. 
        </p> 
        <span class="failureNotification"> 
         <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal> 
        </span> 
        <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" 
         ValidationGroup="RegisterUserValidationGroup"/> 
        <div class="accountInfo"> 
         <fieldset class="register"> 
          <legend>Account Information</legend> 
          <!-- ... other stuff --> 
          <p> 
          <asp:Label ID="SelectRoleLabel" runat="server" AssociatedControlID="RoleDropDownList">Select account type:</asp:Label> 
           <asp:DropDownList ID="RoleDropDownList" runat="server"> 
           </asp:DropDownList> 
          </p> 
         </fieldset> 
         <p class="submitButton"> 
          <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" 
           ValidationGroup="RegisterUserValidationGroup"/> 
         </p> 
        </div> 
       </ContentTemplate> 
       <CustomNavigationTemplate> 
       </CustomNavigationTemplate> 
      </asp:CreateUserWizardStep> 
     </WizardSteps> 
    </asp:CreateUserWizard> 
</asp:Content> 

和後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace myLeMS.Account 
{ 
    public partial class Register : System.Web.UI.Page 
    {   
     protected void Page_Load(object sender, EventArgs e) 
     { 
      RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"]; 

      if (! Page.IsPostBack) 
      { 
       var roleDropDownList = (DropDownList)RegisterUser.CreateUserStep 
        .ContentTemplateContainer.FindControl("RoleDropDownList"); 

       roleDropDownList.DataSource = Roles.GetAllRoles(); 
       roleDropDownList.DataBind(); 
      } 
     } 

     protected void RegisterUser_CreatedUser(object sender, EventArgs e) 
     { 
      FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */); 

      string continueUrl = RegisterUser.ContinueDestinationPageUrl; 
      if (String.IsNullOrEmpty(continueUrl)) 
      { 
       continueUrl = "~/"; 
      } 

      var roleDropDownList = (DropDownList)RegisterUser.CreateUserStep 
       .ContentTemplateContainer.FindControl("RoleDropDownList"); 

      Roles.AddUsersToRole(new string[] { RegisterUser.UserName }, roleDropDownList.SelectedValue); 

      Response.Redirect(continueUrl); 
     } 

    } 
} 

回答

0

你嘗試roleDropDownList.SelectedText? Roles.GetAllRoles只返回一個字符串數組。

+0

DropDownList沒有.SelectedText屬性。 – JaamShikan

+0

我看到...我可以混淆兩個控件。 SelectedItem屬性是否有幫助? –