0

I am trying to allow a user to select a role on createuserwizard from a dropdownlist containing all roles. I dont get errors but user always added to the "Offering Rooms" role no matter what dropdownlist item is selected.

Code:

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

Markup:

<asp:DropDownList ID="RoleDropDownList" runat="server">

                                </asp:DropDownList>

Html:

<option value="Offering Rooms">Offering Rooms</option>
<option value="Seeking Rooms">Seeking Rooms</option>
Phil
  • 1,811
  • 9
  • 38
  • 60

1 Answers1

2

You need to add check if this is post back and not bind again:

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
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30