0

メンバーシップを使用してユーザーを作成し、これにロールを割り当てます。aspxページに次のコードを記述しました... Creatuserフォームウィザードでユーザーの詳細を入力した後、チェックボックス付きのグリッドビューに表示されるユーザーロールを選択する別のオプションがありますが、ロールはgridviewチェックボックスから選択します。最後の値のみを取得し、同じロールを持つユーザーを挿入しますが、選択した値で挿入したい...

これがaspxページのクライアント側コードです..

 <asp:Panel ID="Panel1" runat="server" Style="position: absolute; left: 0px; top: 0px;
        width: 339px; height: 203px;">
        <table width="400">
            <tr>
                <td>
                </td>
                <td colspan="2">
                    <b>Sign Up for New User Account</b>
                </td>
            </tr>
            <tr>
                <td>
                    UserName:
                </td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server" />
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="rqfUserName" runat="server" ControlToValidate="txtUserName"
                        Display="Dynamic" ErrorMessage="Required" ForeColor="Red" />
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                </td>
                <td>
                    <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" />
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtPwd"
                        Display="Dynamic" ErrorMessage="Required" ForeColor="Red" />
                </td>
            </tr>
            <tr>
                <td>
                    Confirm Password:
                </td>
                <td>
                    <asp:TextBox ID="txtCnfPwd" runat="server" TextMode="Password" />
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="PasswordConfirmRequiredValidator" runat="server"
                        ControlToValidate="txtCnfPwd" ForeColor="red" Display="Dynamic" ErrorMessage="Required" />
                    <asp:CompareValidator ID="PasswordConfirmCompareValidator" runat="server" ControlToValidate="txtCnfPwd"
                        ForeColor="red" Display="Dynamic" ControlToCompare="txtPwd" ErrorMessage="Confirm password must match password." />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Create User" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td class="style1" colspan="3">
                    <asp:Label ID="lblResult" runat="server" CausesValidation="false" Font-Bold="true" />
                </td>
                <td class="style2" colspan="1">
                    <asp:Label ID="Label1" runat="server" Visible="false" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
                    BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                    <FooterStyle BackColor="Tan" />
                    <HeaderStyle BackColor="Tan" Font-Bold="True" />
                    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                    <SortedAscendingCellStyle BackColor="#FAFAE7" />
                    <SortedAscendingHeaderStyle BackColor="#DAC09E" />
                    <SortedDescendingCellStyle BackColor="#E1DB9C" />
                    <SortedDescendingHeaderStyle BackColor="#C2A47B" />
                </asp:GridView>
            </tr>
        </table>
    </asp:Panel>

これが私のサーバー側のコードです... aspx.cs

string Role;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindRoles();

    }

    if (IsPostBack)
    {
        getrole();

        showuser();


    }
}

protected void BindRoles()
{
    gvRoles.DataSource = Roles.GetAllRoles();
    gvRoles.DataBind();
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    MembershipCreateStatus createStatus;
    MembershipUser user = Membership.CreateUser(txtUserName.Text, txtPwd.Text, null, null, null, true, out createStatus);
    switch (createStatus)
    {
        //This Case Occured whenver User Created Successfully in database
        case MembershipCreateStatus.Success:



            lblResult.ForeColor = Color.Green;
            lblResult.Text = "The   " + txtUserName.Text + "   account was successfully created with  " +Role+ "";
            txtUserName.Text = string.Empty;

            break;
        // This Case Occured whenver we send duplicate UserName
        case MembershipCreateStatus.DuplicateUserName:
            lblResult.ForeColor = Color.Red;
            lblResult.Text = "The user with the same UserName already exists!";
            break;
        //This Case Occured whenver we give duplicate mail id
        case MembershipCreateStatus.DuplicateEmail:
            lblResult.ForeColor = Color.Red;
            lblResult.Text = "The user with the same email address already exists!";
            break;
        //This Case Occured whenver we send invalid mail format
        case MembershipCreateStatus.InvalidEmail:
            lblResult.ForeColor = Color.Red;
            lblResult.Text = "The email address you provided is invalid.";
            break;
        //This Case Occured whenver we send empty data or Invalid Data
        case MembershipCreateStatus.InvalidAnswer:
            lblResult.ForeColor = Color.Red;
            lblResult.Text = "The security answer was invalid.";
            break;
        // This Case Occured whenver we supplied weak password
        case MembershipCreateStatus.InvalidPassword:
            lblResult.ForeColor = Color.Red;
            lblResult.Text = "The password you provided is invalid. It must be 7 characters long and have at least 1 special character.";
            break;
        default:
            lblResult.ForeColor = Color.Red;
            lblResult.Text = "There was an unknown error; the user account was NOT created.";
            break;
    }

    }

public void getrole()
{

    string roleName = string.Empty;
    foreach (GridViewRow gvrow in gvRoles.Rows)
    {
        CheckBox chk = (CheckBox)gvrow.FindControl("chkRole");
        Label lbl = (Label)gvrow.FindControl("lblRole");
        roleName = lbl.Text;
        Role=roleName;
        if (chk.Checked)
        {
            Roles.AddUserToRole(txtUserName.Text, roleName);
            Label1.ForeColor = Color.Red;
            Label1.Text = "The   " + txtUserName.Text + "  role  " + roleName + "  Added  ";
            //Label1.Visible = true;
        }
    }
}

public void showuser() {

    GridView1.DataSource = Membership.GetAllUsers();
    GridView1.DataBind();
}

私はこの状態で立ち往生しているので、みんな私を助けてください...感謝します..

4

1 に答える 1