0

I am trying to add Role based functionality to my asp.net web-form based website.

I have created Three Roles using ASP.Net Configuration as

  1. Admin
  2. Guest
  3. Editor

and i am using CreateUserWizard to create user i slightly modified it so that we can add Roles also when we create a new User.

Below is the Code from .aspx file and .cs

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
   oncreateduser="CreateUserWizard1_CreatedUser">
   <CreateUserButtonStyle BackColor="#2F5E8C" BorderStyle="None" ForeColor="White" 
      Height="24px" />
   <WizardSteps>
      <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
         <ContentTemplate>
            <table border=0>
               <tr>
                  <td align="right" class="tblRWHeight">
                     <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                  </td>
                  <td class="tblRWHeight">
                     <asp:TextBox ID="UserName" CssClass="txtbox300UserM"  runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                        ControlToValidate="UserName" ErrorMessage="User Name is required." 
                        ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                  </td>
               </tr>
               <tr>
                  <td align="right" class="tblRWHeight">
                     <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                  </td>
                  <td class="tblRWHeight">
                     <asp:TextBox ID="Password" CssClass="txtbox300UserM" runat="server" TextMode="Password"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                        ControlToValidate="Password" ErrorMessage="Password is required." 
                        ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                  </td>
               </tr>
               <tr>
                  <td align="right" class="tblRWHeight">
                     <asp:Label ID="ConfirmPasswordLabel" runat="server" 
                        AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                  </td>
                  <td class="tblRWHeight">
                     <asp:TextBox ID="ConfirmPassword" CssClass="txtbox300UserM" runat="server" TextMode="Password"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                        ControlToValidate="ConfirmPassword" 
                        ErrorMessage="Confirm Password is required." 
                        ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                  </td>
               </tr>
               <tr>
                  <td align="right" class="tblRWHeight">
                     <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                  </td>
                  <td class="tblRWHeight">
                     <asp:TextBox ID="Email" CssClass="txtbox300UserM" runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                        ControlToValidate="Email" ErrorMessage="E-mail is required." 
                        ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                  </td>
               </tr>
               <tr>
                  <td align="center" colspan="2" >
                     <asp:CompareValidator ID="PasswordCompare" runat="server" 
                        ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                        Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." 
                        ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                  </td>
               </tr>
               <tr>
                  <td align="right" class="tblRWHeight">
                     <asp:Label ID="Label1" runat="server" AssociatedControlID="Email">Add Role</asp:Label>
                  </td>
                  <td class="tblRWHeight">
                     <asp:DropDownList ID="ddRoleList" runat="server">  </asp:DropDownList>
                  </td>
               </tr>
               <tr>
                  <td align="center" colspan="2" >
                  </td>
               </tr>
               <tr>
                  <td align="center" colspan="2" style="color:Red;" >
                     <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                  </td>
               </tr>
            </table>
         </ContentTemplate>
      </asp:CreateUserWizardStep>
      <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
      </asp:CompleteWizardStep>
   </WizardSteps>
   <StartNavigationTemplate>
      <asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext" Text="Next" style="background-color:#2F5E8C; border:0px solid #AB1B56; height:24px; color:#fff;" />
   </StartNavigationTemplate>
</asp:CreateUserWizard>

.CS File code to fill drop-down and save role

DropDownList roleDropDownList;
protected void Page_Load(object sender, EventArgs e)
{
//if( (!IsPostBack))
//{
DataSet ds = new DataSet();
ds = DataProvider.Connect_Select(" select RoleName from aspnet_Roles"); 
roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddRoleList");
roleDropDownList.DataSource = ds.Tables[0];
roleDropDownList.DataTextField = "RoleName";
roleDropDownList.DataValueField = "RoleName";
roleDropDownList.DataBind();
//}
}
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, roleDropDownList.SelectedValue);
}

With this code i am able to save the user but it always save Admin as a role in database as it is the first in the drop-down.

I am not sure where i am making the mistake or my approach is wrong I used following link as a reference point

Adding roles to the 'CreateUserWizard'

I would appreciate help in this regard.

UPDATE: Drop-down fills properly

<select class="txtbox300UserM" id="MainContent_CreateUserWizard1_CreateUserStepContainer_ddRoleList" name="ctl00$MainContent$CreateUserWizard1$CreateUserStepContainer$ddRoleList">
    <option value="Admin">Admin</option>
    <option value="Guest">Guest</option>
    <option value="Editor">Editor</option>
</select>

WORKING CODE: I tried similar code before it didnot work for soem reason.. Below code is working...

protected void Page_Load(object sender, EventArgs e)
{
   DropDownList roleDropDownList;
   if (!Page.IsPostBack)
   {
       DataSet ds = new DataSet();
       ds = DataProvider.Connect_Select("select RoleName from aspnet_Roles");
       roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddRoleList");
       roleDropDownList.DataSource = ds.Tables[0];
       roleDropDownList.DataBind();
   }

}

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    string role = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddRoleList")).SelectedItem.Value.ToString();
    Roles.AddUserToRole(CreateUserWizard1.UserName, role);

}
Community
  • 1
  • 1
Learning
  • 19,469
  • 39
  • 180
  • 373

3 Answers3

0

after binding dropdown

roleDropDownList.DataBind();
roleDropDownList.SelectedValue="admin value";
Deepak Saralaya
  • 457
  • 4
  • 12
0

I think this is were it goes wrong

roleDropDownList.DataTextField = "RoleName";
roleDropDownList.DataValueField = "RoleName";

As you can see you selected the Rolename twice.

Try this:

    roleDropDownList.DataTextField = "RoleName";
    roleDropDownList.DataValueField = "ID_RoleName"; 
'or what ever is the unique ID of  the tbl is;
ErikMes
  • 482
  • 2
  • 14
  • But how it it matter even if i select RoleName it should still get the the Name from dropdown. My dropdown get the right values Check Update of question – Learning Jun 12 '13 at 12:40
0

Working Code, I tried similar code before for some reason it didn't work. After many change & looking around i modified code with small changes & it started now it works fine.

protected void Page_Load(object sender, EventArgs e)
{
   DropDownList roleDropDownList;
   if (!Page.IsPostBack)
   {
       DataSet ds = new DataSet();
       ds = DataProvider.Connect_Select("select RoleName from aspnet_Roles");
       roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddRoleList");
       roleDropDownList.DataSource = ds.Tables[0];
       roleDropDownList.DataBind();
   }

}

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    string role = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddRoleList")).SelectedItem.Value.ToString();
    Roles.AddUserToRole(CreateUserWizard1.UserName, role);

}
Learning
  • 19,469
  • 39
  • 180
  • 373