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

📄 commonservice.cs

📁 企业管理信息化之财务管理系统
💻 CS
📖 第 1 页 / 共 4 页
字号:
using System;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using com.unicafe.common;
using com.unicafe.workflow;
using System.Web.Security;

namespace com.ascs.plp.publics
{
	/// <summary>
	/// DataGridPager 的摘要说明。
	/// </summary>
	public class CommonService
	{
		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 构造函数
		/// </summary>
		/// **************************************************************************
		public CommonService()
		{
		}
		/// **************************************************************************
		/// END
		/// **************************************************************************








		//---------------------------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------------------------
		//以下是对主页面的处理方法集
		//---------------------------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------------------------



		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 对首次加载的页面进行处理
		/// </summary>
		/// <param name="thePage">调用此方法的页面对象,建议直接使用this关键字</param>
		/// <param name="sql">用于生成绑定DataGrid的数据集的SQL语句</param>
		/// <param name="theDG">被绑定的DataGrid控件对象</param>
		/// <param name="SessionName">用于与服务器进行交互的Session名称,值为"Data"或"Data1"</param>
		/// <param name="PreviousText">“上一页”文本控件</param>
		/// <param name="NextText">“下一页”文本控件</param>
		/// <param name="PageInfo">“当前第m页/共n页”文本控件</param>
 		/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
		/// **************************************************************************
		public static bool HandleDataGrid(	System.Web.UI.Page thePage, 
											string sql, 
											System.Web.UI.WebControls.DataGrid theDG, 
											string SessionName, 
											System.Web.UI.WebControls.Label PreviousText, 
											System.Web.UI.WebControls.Label NextText, 
											System.Web.UI.WebControls.Label PageInfo)
		{
			//绑定DataGrid控件
			if(BindDataGrid(thePage, sql, theDG, SessionName, false) == false) return false;
			
			//检查是否需要定位
			if(LocationDataGrid(thePage, theDG, SessionName) == false) return false;

			//给导航文本赋值
			if(PageNavigatorText(theDG, PreviousText, NextText, PageInfo) == false) return false;

			return true;
		}
		/// **************************************************************************
		/// END
		/// **************************************************************************





		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 改变查询条件后,重新查询数据库并重新绑定
		/// </summary>
		/// <param name="thePage">调用此方法的页面对象,建议直接使用this关键字</param>
		/// <param name="sql">用于生成绑定DataGrid的数据集的SQL语句</param>
		/// <param name="theDG">被绑定的DataGrid控件对象</param>
		/// <param name="SessionName">用于与服务器进行交互的Session名称,值为"Data"或"Data1"</param>
		/// <param name="PreviousText">“上一页”文本控件</param>
		/// <param name="NextText">“下一页”文本控件</param>
		/// <param name="PageInfo">“当前第m页/共n页”文本控件</param>
		/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
		/// **************************************************************************
		public static bool ReBindDataGrid(	System.Web.UI.Page thePage, 
											string sql, 
											System.Web.UI.WebControls.DataGrid theDG, 
											string SessionName, 
											System.Web.UI.WebControls.Label PreviousText, 
											System.Web.UI.WebControls.Label NextText, 
											System.Web.UI.WebControls.Label PageInfo)
		{
			//绑定DataGrid控件
			if(BindDataGrid(thePage, sql, theDG, SessionName, false) == false) return false;
			
			//给导航文本赋值
			if(PageNavigatorText(theDG, PreviousText, NextText, PageInfo) == false) return false;

			return true;
		}
		/// **************************************************************************
		/// END
		/// **************************************************************************





		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 对DataGrid控件的分页导航链接文本进行赋值
		/// </summary>
		/// <param name="theDG">DataGrid控件对象</param>
		/// <param name="PreviousText">“上一页”文本控件</param>
		/// <param name="NextText">“下一页”文本控件</param>
		/// <param name="PageInfo">“当前第m页/共n页”文本控件</param>
		/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
		/// **************************************************************************
		public static bool PageNavigatorText(DataGrid theDG, Label PreviousText, Label NextText, Label PageInfo)
		{
			try
			{
				//给上一页赋值
				if (theDG.CurrentPageIndex == 0)
				{
					PreviousText.Text = "上一页";
				}
				else
				{
					PreviousText.Text = "<a href=\"javascript:__doPostBack('" + PreviousText.ClientID +"','')\">上一页</a>";
				}
			
				//给下一页赋值
				if ((theDG.CurrentPageIndex + 1) == theDG.PageCount)
				{
					NextText.Text = "下一页";
				}
				else
				{
					NextText.Text = "<a href=\"javascript:__doPostBack('" + NextText.ClientID +"','')\">下一页</a>";
				}

				//给当前第M页/第N页赋值
				PageInfo.Text = "当前第<font id=\"PageIndex\">" + Convert.ToString((theDG.CurrentPageIndex + 1)) + "</font>页 / 共" + theDG.PageCount.ToString() + "页";
				return true;
			}
			catch(Exception e)
			{
				LogService.Write ("PageNavigatorText(DataGrid theDG, Label PreviousText, Label NextText, Label PageInfo, Label TotalText)");
				LogService.Write ("在对DataGrid控件的分页导航链接进行处理时发生错误。");
				LogService.Write (e.Message);
				return false;
			}
		}
		/// **************************************************************************
		/// END
		/// **************************************************************************





		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 将DataGrid控件与数据查询结果集进行绑定
		/// </summary>
		/// <param name="thePage">调用此方法的页面对象,建议直接使用this关键字</param>
		/// <param name="SqlStatement">用于生成绑定DataGrid的数据集的SQL语句</param>
		/// <param name="theDG">被绑定的DataGrid控件对象</param>
		/// <param name="SessionName">与服务器相交互的Session名称,值为"Data"或"Data1"</param>
		/// <param name="ChangePage">是否需要进行当前页的定位</param>
		/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
		/// **************************************************************************
		public static bool BindDataGrid(System.Web.UI.Page thePage, string SqlStatement, System.Web.UI.WebControls.DataGrid theDG, string SessionName, bool ChangePage)
		{
			//声明并创建一个SqlConnection对象的实例
			SqlConnection Connection = new SqlConnection (com.unicafe.common.Configuration.GetDBConnectionString());
			try
			{
				//声明一个用于存储DataGrid对象总页数的整型变量
				int PageCount;

				//声明并创建一个用于保存数据的DataSet对象
				DataSet ds = new DataSet();	
			
				SqlDataAdapter SqlDA = new SqlDataAdapter(SqlStatement, Connection);
				SqlDA.Fill(ds, "TempDataTable");

				DataTable  theDT;
				theDT = ds.Tables["TempDataTable"];


				//判断是否要进行分页调整操作,如果不调整,则将当前页定为第一页
				if (ChangePage == true)
				{
					//计算总页数
					if (theDT.Rows.Count % theDG.PageSize == 0)
					{
						PageCount = theDT.Rows.Count / theDG.PageSize;
					}
					else
					{
						PageCount = theDT.Rows.Count / theDG.PageSize + 1;
					}
					//如果总页数为零,则当前页设为第一页;如果当前页不为零,且当前页超过了总页数,则将当前页置为总页数;其它情况不予处理
					if (PageCount == 0)
					{
						theDG.CurrentPageIndex = 0;
					}
					else if (theDG.CurrentPageIndex >= PageCount)
					{
						theDG.CurrentPageIndex = PageCount - 1;
					}
					else
					{
						//其它情况不予处理
					}
				}
				else
				{
					//将当前页定为第一页
					theDG.CurrentPageIndex = 0;
				}

				//将theDG与查询结果集进行绑定
				//声明一个用于存储数据集的DataView对象
				System.Data.DataView theDV = new DataView(theDT);
				theDG.DataSource= theDV;
				theDG.DataBind();

				//如果总行数为0,则显示页脚,否则不显示
				if(theDG.Items.Count == 0)
				{
					theDG.ShowFooter = true;
					theDG.FooterStyle.CopyFrom(theDG.ItemStyle);
				}
				else
				{
					theDG.ShowFooter = false;
				}
				
				//添加响应鼠标移动的代码行
				MouseMoveOnDataGrid(theDG);

				//对目标页面的Session变量赋值
				thePage.Session[SessionName] = theDT;

				//关闭数据库连接对象
				Connection.Close();
				return true;
			}
			catch(Exception e)
			{
				if(Connection.State.ToString() == "Open") Connection.Close();
				LogService.Write ("BindDataGrid(System.Web.UI.Page thePage, string SqlStatement, System.Web.UI.WebControls.DataGrid theDG, string SessionName, bool ChangePage)");
				LogService.Write ("在将DataGrid控件与数据查询结果集进行绑定时发生错误。");
				LogService.Write (e.Message);
				return false;
			}
		}
		/// **************************************************************************
		/// END
		/// **************************************************************************


		


		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 将DataGrid控件与数据查询结果集进行绑定
		/// </summary>
		/// <param name="thePage">调用此方法的页面对象,建议直接使用this关键字</param>
		/// <param name="SqlCmd">用于生成绑定DataGrid的数据集的SqlCommand对象</param>
		/// <param name="theDG">被绑定的DataGrid控件对象</param>
		/// <param name="SessionName">与服务器相交互的Session名称,值为"Data"或"Data1"</param>
		/// <param name="ChangePage">是否需要进行当前页的定位</param>
		/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
		/// **************************************************************************
		public static bool BindDataGrid(System.Web.UI.Page thePage, System.Data.SqlClient.SqlCommand SqlCmd, System.Web.UI.WebControls.DataGrid theDG, string SessionName, bool ChangePage)
		{
			//声明并创建一个SqlConnection对象的实例
			SqlConnection Connection = new SqlConnection (com.unicafe.common.Configuration.GetDBConnectionString());
			try
			{
				//声明一个用于存储DataGrid对象总页数的整型变量
				int PageCount;

				//声明并创建一个用于保存数据的DataSet对象
				DataSet ds = new DataSet();	
			
				SqlDataAdapter SqlDA = new SqlDataAdapter(SqlCmd);
				SqlDA.Fill(ds, "TempDataTable");

				//输出到XML文件中
				//ds.WriteXml("c:\\data.xml",System.Data.XmlWriteMode.WriteSchema);

				DataTable  theDT;
				theDT = ds.Tables["TempDataTable"];


				//判断是否要进行分页调整操作,如果不调整,则将当前页定为第一页
				if (ChangePage == true)
				{
					//计算总页数
					if (theDT.Rows.Count % theDG.PageSize == 0)
					{
						PageCount = theDT.Rows.Count / theDG.PageSize;
					}
					else
					{
						PageCount = theDT.Rows.Count / theDG.PageSize + 1;
					}
					//如果总页数为零,则当前页设为第一页;如果当前页不为零,且当前页超过了总页数,则将当前页置为总页数;其它情况不予处理
					if (PageCount == 0)
					{
						theDG.CurrentPageIndex = 0;
					}
					else if (theDG.CurrentPageIndex >= PageCount)
					{
						theDG.CurrentPageIndex = PageCount - 1;
					}
					else
					{
						//其它情况不予处理
					}
				}
				else
				{
					//将当前页定为第一页
					theDG.CurrentPageIndex = 0;
				}

				//将theDG与查询结果集进行绑定
				//声明一个用于存储数据集的DataView对象
				System.Data.DataView theDV = new DataView(theDT);
				theDG.DataSource= theDV;
				theDG.DataBind();

				//如果总行数为0,则显示页脚,否则不显示
				if(theDG.Items.Count == 0)
				{
					theDG.ShowFooter = true;
					theDG.FooterStyle.CopyFrom(theDG.ItemStyle);
				}
				else
				{
					theDG.ShowFooter = false;
				}
				
				//添加响应鼠标移动的代码行
				CommonService.MouseMoveOnDataGrid(theDG);

				//对目标页面的Session变量赋值
				thePage.Session[SessionName] = theDT;

				//关闭数据库连接对象
				Connection.Close();
				return true;
			}
			catch(Exception e)
			{
				if(Connection.State.ToString() == "Open") Connection.Close();
				LogService.Write ("BindDataGrid(System.Web.UI.Page thePage, System.Data.SqlClient.SqlCommand SqlCmd, System.Web.UI.WebControls.DataGrid theDG, string SessionName, bool ChangePage)");
				LogService.Write ("在将DataGrid控件与数据查询结果集进行绑定时发生错误。");
				LogService.Write (e.Message);
				return false;
			}
		}
		/// **************************************************************************
		/// END
		/// **************************************************************************






		/// **************************************************************************
		/// BEIGIN
		/// <summary>
		/// 响应DataGrid控件页面索引改变的方法<br/>
		/// </summary>
		/// <param name="thePage">调用此方法的页面对象,建议直接使用this关键字</param>
		/// <param name="theDG">进行分页导航的DataGrid对象</param>
		/// <param name="SessionName">与服务器相交互的Session名称,值为"Data"或"Data1"</param>
		/// <param name="PreviousText">“上一页”文本</param>
		/// <param name="NextText">“下一页”文本</param>
		/// <param name="PageInfo">“当前第m页”文本</param>
		/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
		/// **************************************************************************
		public static bool PageNavigate(System.Web.UI.Page thePage, DataGrid theDG, string SessionName, Label PreviousText, Label NextText, Label PageInfo)
		{
			System.Data.DataTable theDT;
			System.Data.DataView theDV;
			try
			{
				if (thePage.Request.Form["__EVENTTARGET"].ToString() == PreviousText.ID)
				{
					//转到上一页
					theDG.CurrentPageIndex --;
					theDT = (DataTable)thePage.Session[SessionName];
					theDV = new DataView(theDT);
					theDG.DataSource = theDV;
					theDG.DataBind();

					//添加响应鼠标移动的代码行
					MouseMoveOnDataGrid(theDG);

					//为导航文本赋值
					CommonService.PageNavigatorText(theDG, PreviousText, NextText, PageInfo);
				}
				else if (thePage.Request.Form["__EVENTTARGET"].ToString() == NextText.ID)

⌨️ 快捷键说明

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