【问题标题】:Dropdown always get first value in asp.net web forms下拉菜单总是在 asp.net 网络表单中获得第一个值
【发布时间】:2013-06-12 21:27:31
【问题描述】:

我正在尝试将基于角色的功能添加到我的基于 asp.net 网络表单的网站中。

我使用 ASP.Net 配置创建了三个角色

  1. 管理员
  2. 客人
  3. 编辑器

我正在使用 CreateUserWizard 创建用户,我稍微修改了它,以便我们在创建新用户时也可以添加角色。

以下是 .aspx 文件和 .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文件代码填写下拉并保存角色

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

使用此代码,我可以保存用户,但它始终将 Admin 保存为数据库中的角色,因为它是下拉列表中的第一个角色。

我不确定我在哪里犯了错误或者我的方法是错误的,我使用以下链接作为参考点

Adding roles to the 'CreateUserWizard'

我将不胜感激这方面的帮助。

更新:正确填充下拉菜单

<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>

工作代码:我尝试了类似的代码,但由于某些原因无法正常工作。下面的代码正在工作......

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

}

【问题讨论】:

    标签: asp.net asp.net-membership webforms


    【解决方案1】:

    绑定后下拉

    roleDropDownList.DataBind();
    roleDropDownList.SelectedValue="admin value";
    

    【讨论】:

      【解决方案2】:

      我认为这是它出错了

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

      如您所见,您选择了角色名称两次。

      试试这个:

          roleDropDownList.DataTextField = "RoleName";
          roleDropDownList.DataValueField = "ID_RoleName"; 
      'or what ever is the unique ID of  the tbl is;
      

      【讨论】:

      • 但是即使我选择 RoleName 它仍然应该从下拉列表中获取名称。我的下拉列表获取正确的值检查问题更新
      【解决方案3】:

      工作代码,我之前尝试过类似的代码,但由于某种原因它不起作用。经过多次更改并环顾四周后,我对代码进行了一些小改动,现在它开始运行良好。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        相关资源
        最近更新 更多