Hi, guys. I am puzzling with this code.
I need to set IsApproved = true; for a User (using MembershipUser) when I select a CheckBox in a GridView.
The event handler uxRoleCheckBoxSelector_CheckChanged it is setted on the actual CheckBox.
Could you tell me guys what I am doing wrong?
Script does not generate any Exception but does not work. Thanks for your support!
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);
}
}
After come tweaks and your advices here the script edited. Now is working... Hope can help someone else :-)
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);
}
}