2013-07-01 4 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? –