Click here to Skip to main content
15,896,417 members
Articles / Web Development / ASP.NET

ASP.NET TimeTracker Starter Kits Porting from Windows to Linux (Race to Linux)

Rate me:
Please Sign up or sign in to vote.
2.84/5 (9 votes)
5 Oct 20055 min read 70.7K   388   21  
ASP.NET TimeTracker Starter Kits Porting from Windows to Linux using Mainsoft's Grasshopper
<html><head><link rel=stylesheet href=style.css></head><body><div class=SourcePanel style='font-size:12'><pre style='background-color:white'>
<font color= "blue">using</font> System;
<font color= "blue">using</font> System.Web;
<font color= "blue">using</font> System.Web.UI.WebControls;
<font color= "blue">using</font> ASPNET.StarterKit.TimeTracker.BusinessLogicLayer;
<font color= "blue"></font>
<font color= "blue">namespace</font> ASPNET.StarterKit.TimeTracker.Web
<font color= "blue"></font>{
<font color= "green">    //*********************************************************************</font>
<font color= "green">    //</font>
<font color= "green">    // ProjectDetails.aspx</font>
<font color= "green">    //</font>
<font color= "green">    // The ProjectDetails.aspx page is used to add new projects or make changes to</font>
<font color= "green">    // existing projects.  Project membership are associated using this page.</font>
<font color= "green">    // In addition, project categories are added and changed on this page.</font>
<font color= "green">    // Only members of the Admin or Manager Role can access this page.</font>
<font color= "green">    // Members of the Admin group can modify all projects, while members of the</font>
<font color= "green">    // Managers group can only modify the projects they are managing.</font>
<font color= "green">    //</font>
<font color= "green">    //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">    public class</font> ProjectDetails : System.Web.UI.Page
<font color= "blue">    </font>{
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox    ProjectName;
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox    Description;
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox CompletionDate;
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox    Duration;
<font color= "blue">        protected </font>System.Web.UI.WebControls.ListBox    Members;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button CopyButton;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button AddButton;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button SaveButton;
<font color= "blue">        protected </font>System.Web.UI.WebControls.DropDownList Managers;
<font color= "blue">        protected </font>System.Web.UI.WebControls.DropDownList Projects;
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox CategoryName;
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox    Abbrev;
<font color= "blue">        protected </font>System.Web.UI.WebControls.TextBox    CatDuration;
<font color= "blue">        protected </font>System.Web.UI.WebControls.DataGrid CategoriesGrid;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RequiredFieldValidator ProjectNameRequiredfieldvalidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RequiredFieldValidator ManagerRequiredFieldValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.CompareValidator CompletionDateCompareValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RequiredFieldValidator CompletionDateRequiredFieldValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.CompareValidator DurationCompareValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RequiredFieldValidator DurationRequiredFieldValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.CustomValidator    ProjectsGridCustomValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidatorAbbrev;
<font color= "blue">        protected </font>System.Web.UI.WebControls.CompareValidator CatDurationValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.CustomValidator AbbrevCustomValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Label ErrorMessage;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button CancelButton;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button CancelButton2;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button CancelButton3;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button DeleteButton;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button DeleteButton2;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Button SaveButton2;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RangeValidator RangeValidator1;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RangeValidator RangeValidator2;
<font color= "blue">        protected </font>System.Web.UI.WebControls.Label CategoryErrorMessage;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RegularExpressionValidator CategoryNameValidator;
<font color= "blue">        protected </font>System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
<font color= "blue">        </font>
<font color= "green">        // Contains the id of current project</font>
<font color= "blue">        private </font>int _projID;
<font color= "blue">    </font>
<font color= "blue">        private void</font> Page_Load(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "green">            // Ensure that the visiting user has access to view the current page</font>
<font color= "blue"></font><font color= "blue">            if </font>(TTSecurity.IsInRole(TTUser.UserRoleAdminPMgr) == false) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>Response.Redirect("AccessDenied.aspx?Index=-1", true);
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // Obtain project id from the QueryString.</font>
<font color= "blue">            </font>_projID = (Request.QueryString["id"]==null) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue">            if </font>(!IsPostBack)<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "green">                // The categories collection is stored in session</font>
<font color= "green">                // Also the categories are not saved in the database until the user clicks Save for the whole Project</font>
<font color= "blue">                </font>Session["catArray"] = new CategoriesCollection();
<font color= "blue"></font>
<font color= "blue">                </font>BindManagers();
<font color= "blue">                </font>BindMembers();
<font color= "blue">                </font>BindOtherProjects();
<font color= "blue"></font>
<font color= "green">                // Load project with _projID when project id exists in the QueryString</font>
<font color= "blue"></font><font color= "blue">                if </font>(_projID != 0)<font color= "blue"></font>
<font color= "blue">                    </font>BindProject();
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // Add warning and confirmation when user tries to delete a project</font>
<font color= "blue">            </font>DeleteButton.Attributes.Add("onclick", "return confirm('Deleting a project will also delete all the time entries and categories associated with the project. This deletion cannot be undone. Are you sure you want to delete this project?')");
<font color= "blue">            </font>DeleteButton2.Attributes.Add("onclick", "return confirm('Deleting a project will also delete all the time entries and categories associated with the project. This deletion cannot be undone. Are you sure you want to delete this project?')");
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        </font>#region Web Form Designer generated code
<font color= "blue">        </font>override protected void OnInit(EventArgs e)
<font color= "blue">        </font>{
<font color= "green">            //</font>
<font color= "green">            // CODEGEN: This call is required by the ASP.NET Web Form Designer.</font>
<font color= "green">            //</font>
<font color= "blue">            </font>InitializeComponent();
<font color= "blue">            </font>base.OnInit(e);
<font color= "blue">        </font>}
<font color= "blue">        </font>
<font color= "green">        /// <summary></font>
<font color= "green">        /// Required method for Designer support - do not modify</font>
<font color= "green">        /// the contents of this method with the code editor.</font>
<font color= "green">        /// </summary></font>
<font color= "blue">        private void</font> InitializeComponent()
<font color= "blue">        </font>{    
<font color= "blue">            </font>this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click);
<font color= "blue">            </font>this.CancelButton.Click += new System.EventHandler(this.CancelButton_Click);
<font color= "blue">            </font>this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click);
<font color= "blue">            </font>this.AddButton.Click += new System.EventHandler(this.AddButton_Click);
<font color= "blue">            </font>this.AbbrevCustomValidator.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.AbbrevCustomValidator_ServerValidate);
<font color= "blue">            </font>this.CopyButton.Click += new System.EventHandler(this.CopyButton_Click);
<font color= "blue">            </font>this.CategoriesGrid.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.CategoriesGrid_ItemCreated);
<font color= "blue">            </font>this.CategoriesGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.CategoriesGrid_Sort);
<font color= "blue">            </font>this.ProjectsGridCustomValidator.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.ValidateCategories);
<font color= "blue">            </font>this.SaveButton2.Click += new System.EventHandler(this.SaveButton_Click);
<font color= "blue">            </font>this.CancelButton2.Click += new System.EventHandler(this.CancelButton_Click);
<font color= "blue">            </font>this.DeleteButton2.Click += new System.EventHandler(this.DeleteButton_Click);
<font color= "blue">            </font>this.Load += new System.EventHandler(this.Page_Load);
<font color= "blue"></font>
<font color= "blue">        </font>}
<font color= "blue">        </font>#endregion
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The BindManagers method retrieves the list of users that are in the Managers Role</font>
<font color= "green">        // and then databinds them to the Managers dropdownlist.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> BindManagers()
<font color= "blue">        </font>{
<font color= "blue">            </font>Managers.DataSource = TTUser.ListManagers();
<font color= "blue">            </font>Managers.DataValueField = "UserID";
<font color= "blue">            </font>Managers.DataTextField = "Name";
<font color= "blue">            </font>Managers.DataBind();
<font color= "blue"></font>
<font color= "green">            // insert blank choice on top of the managers dropdownlist.</font>
<font color= "blue">            </font>Managers.Items.Insert(0, new ListItem("Select Manager...", String.Empty));
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The BindMembers method retrieves the list of Users the current user can view and </font>
<font color= "green">        // then databinds them to the Members listbox.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> BindMembers()
<font color= "blue">        </font>{
<font color= "blue">            </font>Members.DataSource = TTUser.GetAllUsers(TTSecurity.GetUserID());
<font color= "blue">            </font>Members.DataValueField = "UserID";
<font color= "blue">            </font>Members.DataTextField = "Name";
<font color= "blue">            </font>Members.DataBind();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The BindOtherProjects method retrieves the list of all projects, databinds to</font>
<font color= "green">        // the Projects dropdownlist, and then remove the current project from that list.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> BindOtherProjects()
<font color= "blue">        </font>{
<font color= "blue">            </font>Projects.DataSource = Project.GetProjects();
<font color= "blue">            </font>Projects.DataTextField = "Name";
<font color= "blue">            </font>Projects.DataValueField = "ProjectID";
<font color= "blue">            </font>Projects.DataBind();
<font color= "blue"></font>
<font color= "green">            // remove current project from list of projects</font>
<font color= "blue">            </font>Projects.Items.Remove(Projects.Items.FindByValue(_projID.ToString()));
<font color= "blue"></font>
<font color= "green">            // Disable "Copy" functionality if there are no projects in the list</font>
<font color= "blue"></font><font color= "blue">            if </font>(Projects.Items.Count == 0)<font color= "blue"></font>
<font color= "blue">                </font>CopyButton.Enabled = false;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The BindProject method retrieves the project with _projID and populates</font>
<font color= "green">        // the web controls with the project's info.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> BindProject()
<font color= "blue">        </font>{
<font color= "blue">            </font>Project project = new Project(_projID);
<font color= "blue">            </font>project.Load();
<font color= "blue"></font>
<font color= "green">            // Populate web controls with the project's info.</font>
<font color= "blue">            </font>ProjectName.Text = project.Name;
<font color= "blue">            </font>Description.Text = project.Description;
<font color= "blue">            </font>CompletionDate.Text = project.EstCompletionDate.ToShortDateString();
<font color= "blue">            </font>Duration.Text = project.EstDuration.ToString();
<font color= "blue">            </font>Managers.Items.FindByValue(project.ManagerUserID.ToString()).Selected = true;
<font color= "blue"></font>
<font color= "green">            // Sets the selected members of the project.</font>
<font color= "blue">            </font>foreach (TTUser user in project.Members)
<font color= "blue">            </font>{
<font color= "blue">                </font>Members.Items.FindByValue(user.UserID.ToString()).Selected = true;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid(project.Categories);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The BindCategoriesGrid method is a helper take takes in a collection of categories </font>
<font color= "green">        // and databinds the Categories datagrid with that collection.</font>
<font color= "green">        //</font>
<font color= "green">        // The categories are also then stored in session.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> BindCategoriesGrid(CategoriesCollection cats)
<font color= "blue">        </font>{
<font color= "green">            // Call method to sort the data before databinding</font>
<font color= "blue">            </font>SortGridData(cats, SortField, SortAscending);
<font color= "blue"></font>
<font color= "blue">            </font>CategoriesGrid.DataSource = cats;
<font color= "blue">            </font>CategoriesGrid.DataBind();
<font color= "blue"></font>
<font color= "green">            // Store the list of categories in session</font>
<font color= "blue">            </font>Session["catArray"] = cats;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The ReturnToProjectList method redirects to the ProjectList.aspx page.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> ReturnToProjectList()
<font color= "blue">        </font>{
<font color= "green">            // Clear the current project categories that are stored in session.</font>
<font color= "blue">            </font>Session["catArray"] = null;
<font color= "blue"></font>
<font color= "green">            // Return user to Project List Page.</font>
<font color= "blue">            </font>int mainIndex = (Request["index"]==null) ? 0 : Convert.ToInt32(Request["index"]);
<font color= "blue">            </font>int adminIndex = (Request["adminindex"]==null) ? 0 : Convert.ToInt32(Request["adminindex"]);
<font color= "blue">            </font>Response.Redirect(String.Format("ProjectList.aspx?index={0}&adminindex={1}", mainIndex, adminIndex), false);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The SortGridData method is a helper method called when databinding the Categories grid </font>
<font color= "green">        // to sort the columns of the grid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> SortGridData(CategoriesCollection list, string sortField, bool asc)
<font color= "blue">        </font>{
<font color= "blue">            </font>CategoriesCollection.CategoryFields sortCol = CategoriesCollection.CategoryFields.InitValue;
<font color= "blue"></font>
<font color= "blue">            </font>switch(sortField)
<font color= "blue">            </font>{
<font color= "blue">                case</font> "Name":
<font color= "blue">                    </font>sortCol = CategoriesCollection.CategoryFields.Name;
<font color= "blue">                    </font>break;
<font color= "blue">                case</font> "Abbrev":
<font color= "blue">                    </font>sortCol = CategoriesCollection.CategoryFields.Abbreviation;
<font color= "blue">                    </font>break;
<font color= "blue">                case</font> "Duration":
<font color= "blue">                    </font>sortCol = CategoriesCollection.CategoryFields.Duration;
<font color= "blue">                    </font>break;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>list.Sort(sortCol, asc);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        private void</font> CancelButton_Click(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>ReturnToProjectList();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The DeleteButton_Click event handler calls a method that deletes the project, </font>
<font color= "green">        // all of its categories, projectmembers and their time entries.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue">        </font>
<font color= "blue">        private void</font> DeleteButton_Click(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>Project.Remove(_projID);
<font color= "blue">            </font>ReturnToProjectList();
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The SaveButton_Click event handler saves the configuration of the project.</font>
<font color= "green">        // It updates if the current project id is not 0 and inserts if it is 0.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> SaveButton_Click(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "green">            // Add the project manager as member of the project.</font>
<font color= "blue">            </font>Members.Items.FindByValue(Managers.SelectedItem.Value).Selected = true;
<font color= "blue"></font>
<font color= "green">            // Make sure that there is at least one category.</font>
<font color= "blue">            </font>ProjectsGridCustomValidator.Validate();
<font color= "blue"></font><font color= "blue">            if </font>(!ProjectsGridCustomValidator.IsValid)<font color= "blue"></font>
<font color= "blue">                </font>return;
<font color= "blue">            </font>
<font color= "green">            // Make sure that there are no duplicate categories.</font>
<font color= "blue">            </font>AbbrevCustomValidator.Validate();
<font color= "blue"></font><font color= "blue">            if </font>(!AbbrevCustomValidator.IsValid)<font color= "blue"></font>
<font color= "blue">                </font>return;
<font color= "blue"></font>
<font color= "green">            // Retrieve the current list of categories from session.</font>
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font>
<font color= "green">            // Get list of members the user selected.</font>
<font color= "blue">            </font>UsersCollection selectedMembers = new UsersCollection();
<font color= "blue">            </font>foreach (ListItem li in Members.Items)
<font color= "blue">            </font>{
<font color= "blue"></font><font color= "blue">                if </font>(li.Selected)<font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    </font>TTUser user = new TTUser();
<font color= "blue">                    </font>user.UserID = Convert.ToInt32(li.Value);
<font color= "blue">                    </font>selectedMembers.Add(user);
<font color= "blue">                </font>}
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>Project prj = new Project(
<font color= "blue">                </font>_projID, 
<font color= "blue">                </font>TTSecurity.CleanStringRegex(ProjectName.Text),
<font color= "blue">                </font>TTSecurity.CleanStringRegex(Description.Text), 
<font color= "blue">                </font>Convert.ToInt32(Managers.SelectedItem.Value), 
<font color= "blue">                </font>Convert.ToDateTime(CompletionDate.Text),
<font color= "blue">                </font>Convert.ToDecimal(Duration.Text)
<font color= "blue">                </font>);
<font color= "blue">            </font>prj.Categories = catArray;
<font color= "blue">            </font>prj.Members = selectedMembers;
<font color= "blue">            </font>
<font color= "blue"></font><font color= "blue">            if </font>(prj.Save())<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>ReturnToProjectList();
<font color= "blue">            </font>}
<font color= "blue"></font><font color= "blue">            else</font><font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>ErrorMessage.Text = "There was an error.  You cannot remove a member with time entries from this project.";
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The AddButton_Click event handler adds a category to the categories collection</font>
<font color= "green">        // in session and displays it in the datagrid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> AddButton_Click(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "green">            // check to make sure duplicated category abbreviations are avoided.</font>
<font color= "blue"></font><font color= "blue">            if </font>(!AbbrevCustomValidator.IsValid)<font color= "blue"></font>
<font color= "blue">                </font>return;
<font color= "blue"></font>
<font color= "green">            // a required field validator is not used for this case because only the add button click should </font>
<font color= "green">            // check for this and not the save button.</font>
<font color= "blue"></font><font color= "blue">            if </font>(CategoryName.Text == "")<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>CategoryErrorMessage.Text = "Category name is required.";
<font color= "blue">                </font>return;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // a required field validator is not used for this case because only the add button click should </font>
<font color= "green">            // check for this and not the save button.</font>
<font color= "blue"></font><font color= "blue">            if </font>(Abbrev.Text == "")<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>CategoryErrorMessage.Text = "Category abbreviation is required.";
<font color= "blue">                </font>return;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font><font color= "blue">            if </font>(catArray == null) <font color= "blue"></font>
<font color= "blue">                </font>catArray = new CategoriesCollection();
<font color= "blue"></font>
<font color= "green">            // categories need a unique identifier when added, since they are not yet saved to </font>
<font color= "green">            // the database but in session, a counter is used to populate their id.</font>
<font color= "blue">            </font>int catID = (Session["catID"] != null) ? (Convert.ToInt32(Session["catID"]) - 1) : -1;
<font color= "blue">            </font>Session["catID"] = catID;
<font color= "blue"></font>
<font color= "blue">            </font>Category cat = new Category();
<font color= "blue">            </font>cat.CategoryID = catID;
<font color= "blue">            </font>cat.Name = TTSecurity.CleanStringRegex(CategoryName.Text);
<font color= "blue">            </font>cat.Abbreviation = TTSecurity.CleanStringRegex(Abbrev.Text);
<font color= "blue">            </font>cat.EstDuration = (CatDuration.Text.Length==0) ? 0 : Convert.ToDecimal(CatDuration.Text);
<font color= "blue">            </font>catArray.Add(cat);
<font color= "blue"></font>
<font color= "green">            // Re-Validate now that users have added a new category.</font>
<font color= "blue">            </font>ProjectsGridCustomValidator.Validate();
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid(catArray);
<font color= "blue"></font>
<font color= "blue">            </font>CategoryName.Text = string.Empty;
<font color= "blue">            </font>Abbrev.Text = string.Empty;
<font color= "blue">            </font>CatDuration.Text = string.Empty;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The CopyButton_Click event handler copies the list of categories from the </font>
<font color= "green">        // selected project in the Projects dropdownlist.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> CopyButton_Click(object sender, System.EventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>int catID;
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font><font color= "blue">            if </font>(catArray == null) <font color= "blue"></font>
<font color= "blue">                </font>catArray = new CategoriesCollection();
<font color= "blue"></font>
<font color= "blue">            </font>CategoriesCollection projcatArray = Project.GetCategories(Convert.ToInt32(Projects.SelectedItem.Value));
<font color= "blue">            </font>foreach (Category cat in projcatArray)
<font color= "blue">            </font>{
<font color= "green">                // categories need a unique identifier when added, since they are not yet saved to </font>
<font color= "green">                // the database but in session, a counter is used to populate their id.</font>
<font color= "blue">                </font>catID = (Session["catID"] != null) ? (Convert.ToInt32(Session["catID"]) + 1) : 1;
<font color= "blue">                </font>Session["catID"] = catID;
<font color= "blue">                </font>cat.CategoryID = catID;
<font color= "blue">                </font>catArray.Add(cat);
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // Re-Validate to make sure duplicate categories are not copied.</font>
<font color= "blue">            </font>ProjectsGridCustomValidator.Validate();
<font color= "blue"></font>
<font color= "blue">            </font>Session["catArray"] = catArray;
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid(catArray);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The CategoriesGrid_OnDelete event handler deletes a category from the categories grid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected void</font> CategoriesGrid_OnDelete(Object sender, DataGridCommandEventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>int catID = Convert.ToInt32(CategoriesGrid.DataKeys[(int)e.Item.ItemIndex]);
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font>
<font color= "blue">            for</font> (int i = 0;i < catArray.Count;i++)
<font color= "blue">            </font>{
<font color= "blue"></font><font color= "blue">                if </font>(((Category)catArray[i]).CategoryID == catID)<font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    </font>catArray.RemoveAt(i);
<font color= "blue">                </font>}
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid(catArray);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The CategoriesGrid_OnCancel event handler cancels an edit on the categories grid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected void</font> CategoriesGrid_OnCancel(Object sender, DataGridCommandEventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>CategoriesGrid.EditItemIndex = -1;
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid((CategoriesCollection)Session["catArray"]);        
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The CategoriesGrid_OnUpdate event handler updates changes on an edit of the categories grid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected void</font> CategoriesGrid_OnUpdate(Object sender, DataGridCommandEventArgs e)
<font color= "blue">        </font>{
<font color= "green">            // set up the editted category</font>
<font color= "blue">            </font>Category editCat = new Category();
<font color= "blue">            </font>editCat.CategoryID = Convert.ToInt32(CategoriesGrid.DataKeys[(int)e.Item.ItemIndex]);
<font color= "blue">            </font>editCat.Name = TTSecurity.CleanStringRegex(((TextBox) e.Item.FindControl("EditName")).Text);
<font color= "blue">            </font>editCat.Abbreviation = TTSecurity.CleanStringRegex(((TextBox) e.Item.FindControl("EditAbbreviation")).Text);
<font color= "blue">            </font>editCat.EstDuration = Convert.ToDecimal(((TextBox) e.Item.FindControl("EditDuration")).Text);
<font color= "blue"></font>
<font color= "green">            // check to make sure that category abbreviations are not being duplicated.</font>
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font><font color= "blue">            if </font>(catArray != null) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>string editAbbrev = ((TextBox) e.Item.FindControl("EditAbbreviation")).Text;
<font color= "blue">                </font>foreach (Category cat in catArray)
<font color= "blue">                </font>{
<font color= "blue"></font><font color= "blue">                    if </font>(cat.Abbreviation == editCat.Abbreviation && cat.CategoryID != editCat.CategoryID)<font color= "blue"></font>
<font color= "blue">                    </font>{
<font color= "blue">                        </font>CategoryErrorMessage.Text = "Duplicate categories abbreviations are not allowed";
<font color= "blue">                        </font>return;
<font color= "blue">                    </font>}
<font color= "blue">                </font>}
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "green">            // container for new list of categories</font>
<font color= "blue">            </font>CategoriesCollection catNew = new CategoriesCollection();
<font color= "blue"></font>
<font color= "green">            // Add the editted category to our a new arraylist</font>
<font color= "blue">            </font>catNew.Add(editCat);
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue">            if </font>(catArray == null) <font color= "blue"></font>
<font color= "blue">                </font>catArray = new CategoriesCollection();
<font color= "blue">            </font>foreach (Category cat in catArray)
<font color= "blue">            </font>{
<font color= "green">                // category id does not exist when users are creating new projects so we cannot use it as comparer here</font>
<font color= "green">                // since categories abbreviation must be unique per project we can use it as a comparer</font>
<font color= "blue"></font><font color= "blue">                if </font>(cat.CategoryID != editCat.CategoryID)<font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    </font>catNew.Add(cat);
<font color= "blue">                </font>}
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            </font>CategoriesGrid.EditItemIndex = -1;
<font color= "blue">            </font>BindCategoriesGrid(catNew);
<font color= "blue">        </font>}
<font color= "blue">        </font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // The CategoriesGrid_OnEdit event handler sets up editing for the categories grid</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        protected void</font> CategoriesGrid_OnEdit(Object sender, DataGridCommandEventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>CategoriesGrid.EditItemIndex = e.Item.ItemIndex;
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid((CategoriesCollection)Session["catArray"]);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // CategoriesGrid_Sort handles sorting of the categories grid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> CategoriesGrid_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
<font color= "blue">        </font>{
<font color= "blue">            </font>SortField = e.SortExpression;
<font color= "blue"></font>
<font color= "blue">            </font>BindCategoriesGrid((CategoriesCollection)Session["catArray"]);
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // Custom Validator event handler to make sure that there are categories in the categories grid.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> ValidateCategories(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
<font color= "blue">        </font>{
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font><font color= "blue">            if </font>(catArray != null) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue"></font><font color= "blue">                if </font>(catArray.Count > 0)<font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    </font>args.IsValid = true;
<font color= "blue">                    </font>return;
<font color= "blue">                </font>}
<font color= "blue">            </font>}
<font color= "blue">            </font>args.IsValid = false;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // Custom Validator event handler to make sure that category abbreviations are not duplicated.</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        private void</font> AbbrevCustomValidator_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
<font color= "blue">        </font>{
<font color= "blue">            </font>CategoriesCollection catArray = (CategoriesCollection)Session["catArray"];
<font color= "blue"></font><font color= "blue">            if </font>(catArray.Count != 0) <font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>catArray.Sort(CategoriesCollection.CategoryFields.Abbreviation, true);
<font color= "blue">                </font>for(int i=0; i < catArray.Count; i++)
<font color= "blue">                </font>{
<font color= "blue"></font><font color= "blue">                    if </font>(((Category)catArray[i]).Abbreviation == Abbrev.Text)<font color= "blue"></font>
<font color= "blue">                    </font>{
<font color= "blue">                        </font>args.IsValid = false;
<font color= "blue">                        </font>return;
<font color= "blue">                    </font>}
<font color= "blue"></font><font color= "blue">                    if </font>(i!=0 && ((Category)catArray[i]).Abbreviation == ((Category)catArray[i-1]).Abbreviation )<font color= "blue"></font>
<font color= "blue">                    </font>{
<font color= "blue">                        </font>args.IsValid = false;
<font color= "blue">                        </font>return;
<font color= "blue">                    </font>}
<font color= "blue">                    </font>
<font color= "blue">                </font>}
<font color= "blue">                </font>
<font color= "blue">                </font>
<font color= "blue">            </font>}
<font color= "blue">            </font>args.IsValid = true;
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        private void</font> CategoriesGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
<font color= "blue">        </font>{
<font color= "green">            // add confirmation on deletion of categories</font>
<font color= "blue"></font><font color= "blue">            if </font>(e.Item.ItemType    == ListItemType.Item || e.Item.ItemType    == ListItemType.AlternatingItem)<font color= "blue"></font>
<font color= "blue">            </font>{
<font color= "blue">                </font>((ImageButton)e.Item.FindControl("CatDeleteButton")).Attributes.Add("onclick", "return confirm('Deleting an existing category will also delete all the time entries and categories associated with the category. Are you sure you want to delete this category?')");
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "blue">        </font>string SortField 
<font color= "blue">        </font>{
<font color= "blue">            get</font> 
<font color= "blue">            </font>{
<font color= "blue">                </font>object o = ViewState["SortField"];
<font color= "blue"></font><font color= "blue">                if </font>(o == null) <font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    return</font> String.Empty;
<font color= "blue">                </font>}
<font color= "blue">                return</font> (string)o;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            set</font> 
<font color= "blue">            </font>{
<font color= "blue"></font><font color= "blue">                if </font>(value == SortField) <font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "green">                    // same as current sort file, toggle sort direction</font>
<font color= "blue">                    </font>SortAscending = !SortAscending;
<font color= "blue">                </font>}
<font color= "blue">                </font>ViewState["SortField"] = value;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue"></font>
<font color= "green">        //*********************************************************************</font>
<font color= "green">        //</font>
<font color= "green">        // SortAscending property is tracked in ViewState</font>
<font color= "green">        //</font>
<font color= "green">        //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue">        </font>bool SortAscending 
<font color= "blue">        </font>{
<font color= "blue">            get</font> 
<font color= "blue">            </font>{
<font color= "blue">                </font>object o = ViewState["SortAscending"];
<font color= "blue"></font><font color= "blue">                if </font>(o == null) <font color= "blue"></font>
<font color= "blue">                </font>{
<font color= "blue">                    return</font> true;
<font color= "blue">                </font>}
<font color= "blue">                return</font> (bool)o;
<font color= "blue">            </font>}
<font color= "blue"></font>
<font color= "blue">            set</font> 
<font color= "blue">            </font>{
<font color= "blue">                </font>ViewState["SortAscending"] = value;
<font color= "blue">            </font>}
<font color= "blue">        </font>}
<font color= "blue">    </font>}
<font color= "blue"></font>}
</pre>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Australia Australia
"Impossible" + "'" + " " = "I'm Possible"

Started programming when i was a kid with 286 computers and Spectrum using BASIC from 1986. There was series of languages like pascal, c, c++, ada, algol, prolog, assembly, java, C#, VB.NET and so on. Then shifted my intrest in Architecture during past 5 years with Rational Suite and UML. Wrote some articles, i was member of month on some sites, top poster(i only answer) of week (actually weeks), won some books as prizes, rated 2nd in ASP.NET and ADO.NET in Australia.

There is simplicity in complexity

Comments and Discussions