    using System;
    using System.Data;
    using System.Web.UI.WebControls;
    using ASPNET.StarterKit.TimeTracker.BusinessLogicLayer;

    namespace ASPNET.StarterKit.TimeTracker.Web
    {
        //*********************************************************
        //
        // List page of users, also allow users to assign different roles.
        // Drills into UserDetail Page.
        //
        //*********************************************************

        public class UserList : System.Web.UI.Page
        {
            protected System.Web.UI.WebControls.Button btnNewProject;
            protected System.Web.UI.WebControls.DataGrid Users;
            protected System.Web.UI.WebControls.Button NewUserButton;

            // Use User component to capture user's input during edit mode
            protected TTUser _userInput = new TTUser(0, string.Empty, string.Empty, string.Empty);

            //*******************************************************
            //
            // The Page_Load server event handler on this page is used
            // to populate information fields on this page
            //
            //*******************************************************

            private void Page_Load(object sender, System.EventArgs e)
            {
                // Ensure that the visiting user has access to view the current page
                if (TTSecurity.IsInRole(TTUser.UserRoleAdministrator) == false)
                {
                    Response.Redirect("AccessDenied.aspx?Index=-1", true);
                }

                if (!Page.IsPostBack)
                {
                    LoadUsers();
                }
            }

            //*******************************************************
            //
            // GetRoles methods returns a list of User Roles from Components.Roles
            //
            //*******************************************************

            protected RolesCollection GetRoles()
            {
                return Roles.GetRoles();
            }

            //*******************************************************
            //
            // LoadUsers method populate the DataGrid with a list of Time Tracker users.
            //
            //*******************************************************

            private void LoadUsers()
            {
                UsersCollection users = TTUser.GetUsers(TTSecurity.GetUserID(), TTSecurity.GetUserRole());

                // Check for sorting when binding grid
                SortGridData(users, SortField, SortAscending);
                Users.DataSource = users;
                Users.DataKeyField = "UserID";
                Users.DataBind();
            }

            //*******************************************************
            //
            // SortGridData methods sorts the Users Grid based on which
            // sort field is being selected.  Also does reverse sorting based on the boolean.
            //
            //*******************************************************

            private void SortGridData(UsersCollection list, string sortField, bool asc)
            {
                UsersCollection.UserFields sortCol = UsersCollection.UserFields.InitValue;

                switch(sortField)
                {
                    case "Name":
                        sortCol = UsersCollection.UserFields.Name;
                        break;
                    case "RoleName":
                        sortCol = UsersCollection.UserFields.RoleName;
                        break;
                }

                list.Sort(sortCol, asc);
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                //
                // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                //
                InitializeComponent();
                base.OnInit(e);
            }

            ///
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            ///
            private void InitializeComponent()
            {
                this.NewUserButton.Click += new System.EventHandler(this.NewUserButton_Click);
                this.Users.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Users_PageIndexChanged);
                this.Users.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.Users_Sort);
                this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            //*******************************************************
            //
            // Users_Sort server event handler on this page is used to
            // sorts the DataGrid according to the sort field
            // being selected.
            //
            //*******************************************************

            private void Users_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
            {
                SortField = e.SortExpression;
                LoadUsers();
            }

            //*******************************************************
            //
            // Users_PageIndexChanged server event handler on this page is used
            // for changing page index
            //
            //*******************************************************

            private void Users_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                Users.EditItemIndex= -1;
                Users.CurrentPageIndex = e.NewPageIndex;
                LoadUsers();
            }

            private void NewUserButton_Click(object sender, System.EventArgs e)
            {
                int mainIndex = (Request["index"]==null) ? 0 : Convert.ToInt32(Request["index"]);
                int adminIndex = (Request["adminindex"]==null) ? 0 : Convert.ToInt32(Request["adminindex"]);
                Response.Redirect(String.Format("UserDetail.aspx?index={0}&adminindex={1}", mainIndex, adminIndex), false);
            }

            string SortField
            {
                get
                {
                    object o = ViewState["SortField"];
                    if (o == null)
                        return string.Empty;

                    return (string)o;
                }

                set
                {
                    // Same as current sort file, toggle sort direction
                    if (value == SortField)
                        SortAscending = !SortAscending;

                    ViewState["SortField"] = value;
                }
            }

            //*********************************************************************
            //
            // SortAscending property is tracked in ViewState
            //
            //*********************************************************************
            bool SortAscending
            {
                get
                {
                    object o = ViewState["SortAscending"];
                    if (o == null)
                        return true;

                    return (bool)o;
                }

                set
                {
                    ViewState["SortAscending"] = value;
                }
            }
        }
    }