📄 webformbase.aspx.cs
字号:
using System;
using System.Collections;
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;
namespace WorkGroupManager
{
/// <summary>
/// WebFormBase 的摘要说明。
/// </summary>
public class WebFormBase : System.Web.UI.Page
{
protected BaseSys.BaseService BaseService=new BaseSys.BaseService();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// **************************************************************************
/// 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 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,string[] OrderType)
{
//绑定DataGrid控件
if(BindDataGrid(thePage, sql, theDG, SessionName, false,OrderType) == 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>
/// 将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 bool BindDataGrid(System.Web.UI.Page thePage, string SqlStatement, System.Web.UI.WebControls.DataGrid theDG, string SessionName, bool ChangePage,string[] OrderType)
{
try
{
//声明一个用于存储DataGrid对象总页数的整型变量
int PageCount;
//声明并创建一个用于保存数据的DataSet对象
SqlStatement=this.CreateSqlAddOrder(SqlStatement,OrderType);
DataSet ds = this.BaseService.CreateDataSetFromSql(SqlStatement);
DataTable theDT;
theDT = ds.Tables[0];
//判断是否要进行分页调整操作,如果不调整,则将当前页定为第一页
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;
return true;
}
catch(Exception e)
{
this.BaseService.Write ("BindDataGrid(System.Web.UI.Page thePage, string SqlStatement, System.Web.UI.WebControls.DataGrid theDG, string SessionName, bool ChangePage)");
this.BaseService.Write ("在将DataGrid控件与数据查询结果集进行绑定时发生错误。");
this.BaseService.Write (e.Message);
return false;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
///
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 将DataGrid定位到特定的索引页上
/// </summary>
/// <param name="thePage">调用此方法的页面对象,建议使用this关键字</param>
/// <param name="theDG">被定位的DataGrid对象</param>
/// <param name="SessionName">用于和服务器进行交互的Session名称,建议使用"Data"和"Data1"两者中的一个</param>
/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
/// **************************************************************************
public bool LocationDataGrid(System.Web.UI.Page thePage, System.Web.UI.WebControls.DataGrid theDG, string SessionName)
{
System.Data.DataTable theDT;
System.Data.DataView theDV;
int PageIndex;
try
{
try
{
PageIndex = int.Parse(thePage.Request.QueryString["PageIndex"]) - 1;
}
catch
{
return true;
}
if(PageIndex >= theDG.PageCount)
{
theDG.CurrentPageIndex = theDG.PageCount - 1;
}
else
{
theDG.CurrentPageIndex = PageIndex;
}
theDT = (DataTable)thePage.Session[SessionName];
theDV = new DataView(theDT);
theDG.DataSource = theDV;
theDG.DataBind();
//添加响应鼠标移动的代码行
MouseMoveOnDataGrid(theDG);
return true;
}
catch(Exception e)
{
//不用定位
this.BaseService.Write ("LocationDataGrid(System.Web.UI.Page thePage, DataGrid theDG, string SessionName)");
this.BaseService.Write ("在将DataGrid定位到特定的索引页上时发生错误。");
this.BaseService.Write (e.Message);
return false;
}
}
/// **************************************************************************
/// 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 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)
{
this.BaseService.Write ("PageNavigatorText(DataGrid theDG, Label PreviousText, Label NextText, Label PageInfo, Label TotalText)");
this.BaseService.Write ("在对DataGrid控件的分页导航链接进行处理时发生错误。");
this.BaseService.Write (e.Message);
return false;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
///
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 为DataGrid添加响应鼠标移动的代码
/// </summary>
/// <param name="theDG">被添加鼠标移动响应代码的DataGrid控件</param>
/// **************************************************************************
public static void MouseMoveOnDataGrid(System.Web.UI.WebControls.DataGrid theDG)
{
for (int i=0; i<theDG.Items.Count; i++)
{
theDG.Items[i].Attributes["onMouseOver"] = "javascript:this.bgColor='LemonChiffon'; ";
theDG.Items[i].Attributes["onMouseOut"] = "javascript:this.bgColor='white';";
theDG.Items[i].Attributes["ondblclick"] = "javascript:Select(this);";
}
}
/// **************************************************************************
/// 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 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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -