我试图允许用户从包含所有角色的下拉列表中选择createuserwizard上的角色。我没有收到错误,但无论选择了什么下拉列表项,用户总是会添加到“发布室”角色。
代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
roleDropDownList = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")
roleDropDownList.DataSource = Roles.GetAllRoles()
roleDropDownList.DataBind()
End Sub
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
Roles.AddUserToRole(RegisterUser.UserName, roleDropDownList.SelectedValue)
End Sub
标记:
<asp:DropDownList ID="RoleDropDownList" runat="server">
</asp:DropDownList>
HTML:
<option value="Offering Rooms">Offering Rooms</option>
<option value="Seeking Rooms">Seeking Rooms</option>
答案 0 :(得分:2)
您需要添加检查是否回发并且不再绑定:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
roleDropDownList = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")
roleDropDownList.DataSource = Roles.GetAllRoles()
roleDropDownList.DataBind()
End If
End Sub