views:

13

answers:

0

Hi, I am new in ASP.NET.

I need to SET MembershipUser.IsApproved Property using a CHECKBOX in a GRIDVIEW.

With my code I am not able to make it. Could you guys have an idea? Please if you can Update some code. Thanks for your time!

ASP PAGE


<asp:GridView ID="uxUserListDisplayer" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="uxUserListDisplayer_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Active">
            <ItemTemplate>
                <asp:CheckBox ID="uxActiveCheckBoxSelector" runat="server" AutoPostBack="true" OnCheckedChanged="uxRoleCheckBoxSelector_CheckChanged" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Users">
            <ItemTemplate>
                <asp:Label runat="server" ID="uxUserNameLabelDisplayer" Text='<%# Container.DataItem %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkEditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CODE BEHIND


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Security;
using System.Data;

namespace Cms.AdminCms.Sections.CmsMembership
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        private void BindUsersToUserList()
        {
            // Get all of the user accounts as Object     
            MembershipUserCollection users = Membership.GetAllUsers();
            uxUserListDisplayer.DataSource = users;
            uxUserListDisplayer.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)   //Page has been load the first time NO PostBack
            {
                // Data Bind for GirdView
                BindUsersToUserList();
            }
        }

        protected void uxUserListDisplayer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Check or Uncheck a CheckBox depending if user is Approved
            switch (e.Row.RowType)
            {
                // In case type of row is DataRow (a data row of GridView) 
                case DataControlRowType.DataRow:
                    // Create an object of type MembershipUser for row bounded (Users)
                    MembershipUser myUser = (MembershipUser)e.Row.DataItem;
                    // Find out the edit button "uxLinkEditButton" and create an object for it
                    LinkButton editButton = (LinkButton)e.Row.FindControl("uxLinkEditButton");
                    // Find out the checkbox "uxActiveCheckBoxSelector" and create an object for it
                    CheckBox activeCheckBox = (CheckBox)e.Row.FindControl("uxActiveCheckBoxSelector");
                    // Check if the Object type MembershipUser for a User is approved or not
                    if (myUser.IsApproved == true)
                    {
                        activeCheckBox.Checked = true; // Checkbox is checked
                    }
                    else
                    {
                        activeCheckBox.Checked = false; // Checkbox is unchecked
                    }
                    break;
            }
        }

        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 User in a row
            MembershipUser myUser = (MembershipUser)row.DataItem;
            Label myUserName = (Label)row.FindControl("uxUserNameLabelDisplayer");

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

    }
}