⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.aspx.cs

📁 一个采用VS2008+Sql2000开发的任务管理系统
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace BronzeMonkey.GeneralTaskList
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public partial class TaskListIndex : System.Web.UI.Page
	{
	
		//Our web service
		private TaskList tl = new TaskList();

		private UserInformation CurrentUser = new UserInformation();

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (Session["CurrentUser"] == null) 
				Response.Redirect("login.aspx");
			else
				CurrentUser = (UserInformation)Session["CurrentUser"];

			if (!Page.IsPostBack)
			{
				CacheFontTagsForTaskSummary();
				LoadTaskListDropDown(cboTaskLists);
				DetermineMenusToShow();
				ShowTaskList(Convert.ToInt32(cboTaskLists.SelectedItem.Value));
			}
		}


		#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);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

		}
		#endregion

		private void DetermineMenusToShow()
		{
			lnkManageTaskLists.Visible = CurrentUser.IsManager; imgManageTaskLists.Visible = CurrentUser.IsManager;
			lnkManageUsers.Visible = CurrentUser.IsAdministrator; imgManageUsers.Visible = CurrentUser.IsAdministrator;
			lnkManageApplication.Visible = CurrentUser.IsAdministrator; imgManageApplication.Visible = CurrentUser.IsAdministrator;
      lnkManageNotification.Visible = CurrentUser.IsAdministrator; imgManageNotification.Visible = CurrentUser.IsAdministrator;

			AdministrativeTasksTable.Visible = false;
			AdministrativeTasksHeader.Visible = false;

			if (CurrentUser.IsManager || CurrentUser.IsAdministrator)
			{
				AdministrativeTasksTable.Visible = true;
				AdministrativeTasksHeader.Visible = true;
			}
		}

		private void LoadTaskListDropDown(DropDownList cboTaskLists)
		{
			SqlDataReader dr = tl.GetUserTaskLists(CurrentUser, CurrentUser.UserID);

			cboTaskLists.Items.Clear();

			while (dr.Read())
			{
				cboTaskLists.Items.Add(new ListItem(dr["TaskListName"].ToString(), dr["TaskListID"].ToString()));
			}
			
			cboTaskLists.SelectedIndex = -1;
			if (Session["TaskListID"] != null)
			{
				if (cboTaskLists.Items.FindByValue(Session["TaskListID"].ToString()) != null)	
					cboTaskLists.Items.FindByValue(Session["TaskListID"].ToString()).Selected = true;
			}
			else
			{
				//Get the startup task list
				int TaskListIDToShow;
				
				TaskListIDToShow = tl.GetStartupTaskListID(CurrentUser);

				if (cboTaskLists.Items.FindByValue(TaskListIDToShow.ToString()) != null)
					cboTaskLists.Items.FindByValue(TaskListIDToShow.ToString()).Selected = true;
			}
			if (cboTaskLists.SelectedItem.Value != String.Empty) Session["TaskListID"] = Convert.ToInt32(cboTaskLists.SelectedItem.Value);
		}

		/// <summary>
		/// Retrieve a list of the font tags that we flag our task summary headers with, and cache them
		/// in Session[].  We do this so that we can draw the fonts appropriately when building the grid, and
		/// we can do it dynamically by changing the text in the TaskListStatus table in the database.
		/// The usercontrol TaskSummary.ascx uses the Session value Session["StatusFontFlagsCollection"] to
		/// render the fonts on the grid.
		/// </summary>
		private void CacheFontTagsForTaskSummary()
		{
			NameValueCollection StatusFontFlags = new NameValueCollection();
		
			SqlDataReader dr = tl.GetStatusList(CurrentUser);
			while (dr.Read())
			{
				StatusFontFlags.Add(dr["Description"].ToString(), dr["FontFlags"].ToString());
			}
			Session["StatusFontFlagsCollection"] = StatusFontFlags;
		}

		/// <summary>
		/// Retrieves a list of the status codes in the database, and uses them to populate
		/// a drop-down list.  This gets executed when a user clicks the "View" button.
		/// </summary>
		/// <param name="StatusList">The DropDownList to populate with Status Codes.</param>
		private void LoadStatusList(DropDownList StatusList)
		{
			SqlDataReader dr = tl.GetStatusList(CurrentUser);
			while (dr.Read())
			{
				StatusList.Items.Add(new ListItem(dr["Description"].ToString(), dr["Value"].ToString()));
			}
		}

		private void LoadCategoryList(DropDownList CategoryList)
		{
			SqlDataReader dr = tl.GetCategoryList(CurrentUser);
			while( dr.Read() )
			{
				CategoryList.Items.Add(new ListItem(dr["Description"].ToString(), dr["CategoryID"].ToString()));
			}
		}

		/// <summary>
		/// Retrieves a task list and displays it on the DataGrid by using the DataGrid's
		/// DataBinding functionality.
		/// </summary>
		/// <param name="TaskListID">The Task List to retrieve</param>
		private void ShowTaskList(int TaskListID)
		{
			if (TaskListID == 0) return;

			int PreviousPageIndex = DataGrid1.CurrentPageIndex;
			DataGrid1.CurrentPageIndex = 0;
			DataSet ds = tl.GetTaskList(CurrentUser, TaskListID);
			DataView dv = ds.Tables[0].DefaultView;

			if (Session["TaskListSort"] != null) dv.Sort = Session["TaskListSort"].ToString();
			
			string NumberOfItemsToDisplay = tl.GetNumberOfItemsToDisplayPerPage(CurrentUser);
			
			if (NumberOfItemsToDisplay == "All" && dv.Count > 0)
				DataGrid1.PageSize = dv.Count;
			else if(NumberOfItemsToDisplay != "All" && dv.Count > 0)
				DataGrid1.PageSize = Convert.ToInt32(NumberOfItemsToDisplay);
			else
				DataGrid1.PageSize = 20; // default if there are no items to display.

			DataGrid1.DataSource = dv;
			DataGrid1.DataBind();
			DataGrid1.Columns[1].Visible = false;

			if (DataGrid1.PageCount == 1)
				DataGrid1.PagerStyle.Visible = false;
			else
				DataGrid1.PagerStyle.Visible = true;

			if (PreviousPageIndex > (DataGrid1.PageCount - 1)) 
			{
				DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1;
				DataGrid1.DataBind();
			}
			else
			{
				DataGrid1.CurrentPageIndex = PreviousPageIndex;
				DataGrid1.DataBind();
			}
		}

		/// <summary>
		/// This event gets fired when a user selects a new task list.  We load in the new task list.
		/// </summary>
		protected void cboTaskLists_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// Load the task list that corresponds to this task list name
			if (cboTaskLists.SelectedItem.Value != String.Empty)
			{
				Session["TaskListID"] = Convert.ToInt32(cboTaskLists.SelectedItem.Value);
				ShowTaskList(Convert.ToInt32(cboTaskLists.SelectedItem.Value));
			}
		}

		/// <summary>
		/// Destroys our authentication session, removes the Session variables and
		/// redirects the user to the login page.
		/// </summary>
		protected void lnkLogOff_Click(object sender, System.EventArgs e)
		{
			Session.Abandon();
			Response.Cookies.Clear();
			System.Web.Security.FormsAuthentication.SignOut();
			
			Response.Redirect("login.aspx");
		}

		/// <summary>
		/// Redirects the user to the "Create a new Task" page
		/// </summary>
		protected void lnkAdd_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("Task.aspx");
		}

		/// <summary>
		/// This event gets fired when a user clicks the "View" button on the task list display
		/// </summary>
		private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			// The "View/Edit" link
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			ShowTaskList(Convert.ToInt32(cboTaskLists.SelectedItem.Value));
		}

		/// <summary>
		/// This event gets fired when the user clicks the "Hide" button when viewing a task
		/// </summary>
		private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			DataGrid1.DataBind();
			ShowTaskList(Convert.ToInt32(cboTaskLists.SelectedItem.Value));
		}

		/// <summary>
		/// Rebinds the grid and redisplays the tasks.  If two users are working on the same exact task list
		/// from different PCs, they can click the "Refresh" link to see the changes/updates the other user has made.
		/// </summary>
		protected void lnkRefresh_Click(object sender, System.EventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			DataGrid1.DataBind();
			ShowTaskList(Convert.ToInt32(cboTaskLists.SelectedItem.Value));
		}

		/// <summary>
		/// This is a generic event handler that gets fired when any of the Grid's constituent controls fire an event.
		/// We can keep track of what is firing by the CommandName property.  For instance, the "Save" link has a 
		/// CommandName of "SaveTask".
		/// </summary>
		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			TextBox txtSubject, txtBody;
			DropDownList cboStatus;
			DropDownList cboCategory;
      DropDownList cboMoveTo;
			int TaskListID = 0;
			int TaskID = 0;

      if (e.CommandName == "SaveTask")
      {
        TaskID = Convert.ToInt32(e.Item.Cells[1].Text);
        TaskListID = Convert.ToInt32(cboTaskLists.SelectedItem.Value);

        txtSubject = (TextBox)e.Item.FindControl("txtTaskSubject");	
        txtBody = (TextBox)e.Item.FindControl("txtTaskBody");
        cboStatus = (DropDownList)e.Item.FindControl("cboTaskStatus");
        cboCategory = (DropDownList)e.Item.FindControl("cboTaskCategory");
        cboMoveTo = (DropDownList)e.Item.FindControl("cboMoveTo");

        TaskListItem ti = tl.GetTaskListItem(CurrentUser, TaskID, Convert.ToInt32(cboTaskLists.SelectedItem.Value));
        ti.TaskListID = Convert.ToInt32(cboMoveTo.SelectedItem.Value);
        ti.Body = Server.HtmlEncode(txtBody.Text);
        ti.StatusValue = Convert.ToInt32(cboStatus.SelectedItem.Value);
        ti.CategoryID = Convert.ToInt32(cboCategory.SelectedItem.Value);
        ti.StatusName = cboStatus.SelectedItem.Text;
        ti.Subject = Server.HtmlEncode(txtSubject.Text);
        ti.ModifiedDate = DateTime.Now;
        tl.Modify(CurrentUser, ti, TaskListID);
				
        DataGrid1.EditItemIndex = -1;
        this.ShowTaskList(TaskListID);
      }
      else if (e.CommandName == "CancelTask")
      {
        TaskListID = Convert.ToInt32(cboTaskLists.SelectedItem.Value);

        DataGrid1.EditItemIndex = -1;
        this.ShowTaskList(TaskListID);
      }
      else if (e.CommandName == "DeleteTask")
      {
        TaskID = Convert.ToInt32(e.Item.Cells[1].Text);
        TaskListID = Convert.ToInt32(cboTaskLists.SelectedItem.Value);

        tl.Delete(CurrentUser, TaskListID, TaskID);
        DataGrid1.EditItemIndex = -1;
        this.ShowTaskList(TaskListID);
      }
		}

		/// <summary>
		/// This event gets fired when the grid is building.  Also, when the user clicks the "View" button,
		/// it gets fired again as all the new data loads.  This is where we can populate the Status List dropdown list,
		/// as well as the TaskSummary usercontrol object (called SummaryHeader here)
		/// </summary>
		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			DropDownList StatusList = (DropDownList)e.Item.FindControl("cboTaskStatus");
			DropDownList CategoryList = (DropDownList)e.Item.FindControl("cboTaskCategory");
      DropDownList MoveToList = (DropDownList)e.Item.FindControl("cboMoveTo");
			TaskSummary SummaryHeader = (TaskSummary)e.Item.FindControl("TaskSummary");

			DataRowView dr = (DataRowView)e.Item.DataItem;

			if (StatusList != null)
			{
				this.LoadStatusList(StatusList);
				if (StatusList.Items.FindByText(dr["Status"].ToString()) != null)
					StatusList.Items.FindByText(dr["Status"].ToString()).Selected = true;
			}

			if (CategoryList != null)
			{
				this.LoadCategoryList(CategoryList);
				if (CategoryList.Items.FindByValue(dr["CategoryID"].ToString()) != null)
					CategoryList.Items.FindByValue(dr["CategoryID"].ToString()).Selected = true;
			}

      if (MoveToList != null)
      {
        this.LoadTaskListDropDown(MoveToList);
        if (MoveToList.Items.FindByValue(dr["TaskListID"].ToString()) != null)
          MoveToList.Items.FindByValue(dr["TaskListID"].ToString()).Selected = true;
      }

			if (SummaryHeader != null)
			{
				SummaryHeader.Subject = dr["Subject"].ToString();
				SummaryHeader.Status = dr["Status"].ToString();
				SummaryHeader.CreatedOn = (DateTime)dr["CreatedDate"];
				SummaryHeader.LastModified = (DateTime)dr["LastModifiedDate"];
        SummaryHeader.LastModifiedBy = dr["LastModifiedBy"].ToString();
				SummaryHeader.CategoryIconUrl = dr["IconUrl"].ToString();
			}
		}

		/// <summary>
		/// This event gets fired whenever a user changes a page.  We just have to set the CurrentPageIndex to
		/// whatever argument was passed in, then rebind the grid.
		/// </summary>
		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex = e.NewPageIndex;
			this.ShowTaskList(Convert.ToInt32(cboTaskLists.SelectedItem.Value));
		}

		protected void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}

		private void lnkManageTaskLists_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("ManageTaskList.aspx");
		}

		private void lnkManageUsers_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("ManageUsers.aspx");
		}

		protected void lnkMyPreferences_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("Preferences.aspx");
		}

		private void lnkManageApplication_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("ManageApplication.aspx");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -