1

我有一个 DropDownList 用于在注册时选择角色。问题是在 .CreatedUser 事件中,我这样做了

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);
        }

    }
}
4

1 回答 1

0

Did you try roleDropDownList.SelectedText? Roles.GetAllRoles only returns a array of strings.

于 2013-07-01T14:10:13.720 回答