views:

29

answers:

1

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

Are you sure this event is actually getting fired (and thus this method is actually running)?

Do you have a AutoPostBack = true set on the checkbox? If not, and you are using a button to do a postback, I am not sure if that event will get fired for the checkbox... but correct me if I'm wrong.

Chris
Hi, YES the event is getting fired. All the logic is working. AutoPostBack is TRUE for the CheckBox. I tried to debug my code and I and MembershipUser myUser is always on NULL.... any idea???? thanks for your time
GIbboK
can you tell at what point it is not working... like can you put some breakpoints in there and debug and see at what point things go awry? Does the uxMessageDisplayer.Text get changed? (Should if no exceptions are being thrown) If not, check again if this method is actually running. If yes, try to narrow down what part of this isn't working
Chris
Hi Chris, I tested again the script, no exceptions are being thrown, uxMessageDisplayer.Text get changed correctly, events and methods has been used correctly.Using some breakpoints I can confirm you the problem is"MembershipUser myUser = (MembershipUser)user.DataItem;"Value for myUser it is always NULL, so I cannot apply Membership.UpdateUser(myUser);Any ideas? Thanks for your support :-)
GIbboK
Hi Chris, now code is working, I update my question so you can have a look at the code thanks
GIbboK
Great... glad you got it working
Chris