Asp.net MembershipUser从GridView检索数据

时间:2010-10-15 06:24:33

标签: c# asp.net web-controls

我对这段代码感到困惑。

我需要设置IsApproved = true;当我在GridView中选择CheckBox时,用户(使用MembershipUser)。

事件处理程序uxRoleCheckBoxSelector_CheckChanged它是在实际的CheckBox上设置的。

你能告诉我们我做错了什么吗?

脚本不会生成任何异常但不起作用。感谢您的支持!

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

1 个答案:

答案 0 :(得分:0)

您确定此事件实际上已被触发(因此此方法实际上正在运行)吗?

您是否在复选框上设置了AutoPostBack = true?如果没有,并且您正在使用按钮进行回发,我不确定该复选框是否会触发该事件......但如果我错了,请纠正我。