我需要设置IsApproved=true;对于用户(使用MembershipUser),当我在GridView中选中复选框时。
事件处理程序uxRoleCheckBoxSelector\u CheckChanged它是在实际复选框上设置的。
你们能告诉我我做错了什么吗?
protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
{
// Cast sender to CheckBox
CheckBox activeCheckBox = (CheckBox)sender;
// Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;
// Retrive the Lable for an User name in a row
Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");
GridViewRow user = (GridViewRow)myUserName.NamingContainer;
MembershipUser myUser = (MembershipUser)user.DataItem;
if (activeCheckBox.Checked == true)
{
uxMessageDisplayer.Text = "T - Aproved User";
myUser.IsApproved = true;
Membership.UpdateUser(myUser);
}
else
{
uxMessageDisplayer.Text = "F - NOT Aproved User";
myUser.IsApproved = false;
Membership.UpdateUser(myUser);
}
}
之后来调整和你的意见在这里的脚本编辑。现在正在工作。。。希望能帮助别人:-)
protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
{
// Cast sender to CheckBox
CheckBox activeCheckBox = (CheckBox)sender;
// Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;
// Retrive the name for the User from a label
Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");
// Keep User's name in a variable
string UserName = myUserName.Text;
// Create an Object of type MembershipUser and associate its User's name
MembershipUser myUser = Membership.GetUser(UserName);
// Check if a CheckBox is selected or not for a User
if (activeCheckBox.Checked == true)
{
// Set status for an User
myUser.IsApproved = true;
// Save status
Membership.UpdateUser(myUser);
// Display message
uxMessageDisplayer.Text = string.Format("The User {0} has been activated.", UserName);
}
else
{
myUser.IsApproved = false;
Membership.UpdateUser(myUser);
uxMessageDisplayer.Text = string.Format("The User {0} has been deactivated. User cannot use this System.", UserName);
}
}