我使用会员资格来创建用户并为此分配角色我在我的aspx页面中编写了以下代码...在Creatuser表单向导中输入用户详细信息后,还有另一个选项可以选择用户角色,该选项显示在gridview中,带有复选框,但无论如何角色你从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" />
</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();
}
请大家帮助我,因为我被困在这种情况下......会很感激......