Geeks With Blogs
The Trials and Tribulations of a Self-Taught Developer By Justin Russell

OK, here is a nice chunk of code for a web part that will display the accounts within a Forms Based Authentication database. The code is complete all the bells and whistles including a context menu, paging, sorting, and searching. I couldn't get filtering to work so I opted for searching instead. It turns out searching will be a much more beneficial feature because selecting a username to filter on from a list of several hundred items would be pretty inefficient. This code should pretty much plug into a web part. I'm posting it here for reference - if someone needs clarification, post your question in the comments.

I apologize in advance for the line wrapping caused by the lack of width available in my blog. If you can't get a good copy and paste let me know and I'll put it somewhere as a text file.

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.Security;

namespace FBAUserManagement
    public class FBAUserManagement : System.Web.UI.WebControls.WebParts.WebPart
        DataSet oDataSet = new DataSet();
        DataTable dt = new DataTable();
        DataView oView = new DataView();
        SPGridView oGrid = new SPGridView();
        Table searchTable = new Table();
        TextBox txtUserSearch = new TextBox();
        Button btnSearch = new Button();

        public FBAUserManagement()


        protected override void CreateChildControls()
                //Set up the search table appearing above the SPGridView
                searchTable.ID = "navTable";
                txtUserSearch.ID = "txtUserSearch";
                btnSearch.ID = "btnSearch";
                btnSearch.Text = "Search)";
                TableRow tr1 = new TableRow();
                TableCell tc1 = new TableCell();
                tc1.Text = "Search (e-mail):";
                TableCell tc2 = new TableCell();
                TableCell tc3 = new TableCell();

                oView.Table = dt;
                oGrid.ID = "UserGrid";
                oGrid.DataSource = oView;
                oGrid.AutoGenerateColumns = false;
                oGrid.AllowSorting = true;
                oGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);

                //Add the UserName column to the DataView
                SPMenuField colMenu = new SPMenuField();
                colMenu.HeaderText = "User Name";
                colMenu.TextFields = "UserName";
                colMenu.MenuTemplateId = "UserNameListMenu";
                colMenu.NavigateUrlFields = "UserName";
                colMenu.NavigateUrlFormat = "do.aspx?p={0}";
                colMenu.TokenNameAndValueFields = "NAME=UserName";
                colMenu.SortExpression = "UserName";

                //Build the Contenx Menu
                //Images used are all native to SharePoint
                MenuTemplate UserNameListMenu = new MenuTemplate();
                UserNameListMenu.ID = "UserNameListMenu";

                MenuItemTemplate userDisable = new MenuItemTemplate("Disable Account", "/_layouts/images/ServiceNotInstalled.gif");
                userDisable.ID = "userDisable";
                userDisable.ClientOnClickNavigateUrl = "do.aspx?this=%EDIT%that=%NAME%";

                MenuItemTemplate userUnlock = new MenuItemTemplate("Unlock Account", "/_layouts/images/ServiceInstalled.gif");
                userUnlock.ID = "userUnlock";
                userUnlock.ClientOnClickNavigateUrl = "do.aspx?this=%EDIT%that=%NAME%";

                MenuItemTemplate userReset = new MenuItemTemplate("Reset Password", "/_layouts/images/recurrence.gif");
                userReset.ID = "userReset";
                userReset.ClientOnClickNavigateUrl = "do.aspx?this=%EDIT%that=%NAME%";


                //Add E-mail Column
                BoundField emailLink = new BoundField();
                emailLink.DataField = "Email";
                emailLink.HeaderText = "E-mail";
                emailLink.SortExpression = "Email";

                //Add Locked Out Column
                CheckBoxField isLockedOut = new CheckBoxField();
                isLockedOut.DataField = "IsLockedOut";
                isLockedOut.HeaderText = "Locked Out";
                isLockedOut.SortExpression = "IsLockedOut";

                //Add Last Login Column
                BoundField LastLogin = new BoundField();
                LastLogin.DataField = "LastLoginDate";
                LastLogin.HeaderText = "Last Login";
                LastLogin.SortExpression = "LastLoginDate";

                //Add Last Password Change Column
                BoundField LastPasswordChange = new BoundField();
                LastPasswordChange.DataField = "LastPasswordChangedDate";
                LastPasswordChange.HeaderText = "Last Password Change";
                LastPasswordChange.SortExpression = "LastPasswordChangedDate";


                //Turn on Paging and add event handler
                oGrid.PageSize = 10;
                oGrid.AllowPaging = true;
                oGrid.PageIndexChanging += new GridViewPageEventHandler(oGrid_PageIndexChanging);
                oGrid.PagerTemplate = null; // Must be called after Controls.Add(oGrid)

                //Recreate current sort if needed
                if (ViewState["SortDirection"] != null && ViewState["SortExpression"] != null)
                    //We have sorting so it needs to be preserved
                    oView.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();

            catch (Exception e)

        void oGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
            oGrid.PageIndex = e.NewPageIndex;

        private void ApplyFilter()
            if (txtUserSearch.Text != "")
                oView.RowFilter = string.Format("Email LIKE '%{0}%'", txtUserSearch.Text);

        protected override void Render(HtmlTextWriter writer)
            //If there is a filter it must be applied before binding

        private void oGrid_Sorting(object sender, GridViewSortEventArgs e)
            //Sorting Logic
            string lastExpression = "";
            if (ViewState["SortExpression"] != null)
                lastExpression = ViewState["SortExpression"].ToString();

            string lastDirection = "asc";
            if (ViewState["SortDirection"] != null)
                lastDirection = ViewState["SortDirection"].ToString();

            string newDirection = "asc";
            if (e.SortExpression == lastExpression)
                newDirection = (lastDirection == "asc") ? "desc" : "asc";

            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = newDirection;

            oView.Sort = e.SortExpression + " " + newDirection;


        private void PopulateDataset()
            //Pull each user in the membership into a DataSet
            oDataSet.ExtendedProperties.Add("ID", "MyDataSet");
            dt = oDataSet.Tables.Add("Users");
            MembershipUserCollection muc;
            muc = Membership.GetAllUsers();
            int counter = 0;
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dt.Columns.Add("UserName", Type.GetType("System.String"));
            dt.Columns.Add("Email", Type.GetType("System.String"));
            dt.Columns.Add("isLockedOut", Type.GetType("System.Boolean"));
            dt.Columns.Add("LastLoginDate", Type.GetType("System.DateTime"));
            dt.Columns.Add("LastPasswordChangedDate", Type.GetType("System.DateTime"));
            foreach (MembershipUser mu in muc)
                DataRow dr;
                dr = dt.NewRow();
                dr["ID"] = counter;
                dr["UserName"] = mu.UserName;
                dr["Email"] = mu.Email;
                dr["isLockedOut"] = mu.IsLockedOut;
                dr["LastLoginDate"] = mu.LastLoginDate;
                dr["LastPasswordChangedDate"] = mu.LastPasswordChangedDate;


Posted on Tuesday, July 29, 2008 5:04 PM | Back to top

Comments on this post: SPGridView for FBA with Context Menu's, Paging, Sorting, and Searching

# re: SPGridView for FBA with Context Menu's, Paging, Sorting, and Searching
Requesting Gravatar...
I am use this code and buid. use this webpart in my porta site geting SQL Connection error below how to resolve this error help me and thanks for this give code

"System.Web.HttpException: Unable to connect to SQL Server database. ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) at System.Data.SqlClient.SqlInternalConnection"
Left by Robert on Feb 09, 2009 3:16 AM

Your comment:
 (will show your gravatar)

Copyright © Justin Russell | Powered by: