模板中的DropDownList.SelectedValue为空

时间:2013-07-01 14:03:12

标签: asp.net drop-down-menu selectedvalue

我有一个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);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过roleDropDownList.SelectedText? Roles.GetAllRoles只返回一个字符串数组。