?? commonservice.cs
字號:
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 + -