?? commonservice.cs
字號:
{
//轉到下一頁
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
{
}
return true;
}
catch(Exception e)
{
LogService.Write ("PageNavigate(System.Web.UI.Page thePage, DataGrid theDG, string SessionName, Label PreviousText, Label NextText, Label PageInfo)");
LogService.Write ("響應DataGrid控件頁面索引改變的方法發生錯誤。");
LogService.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 static 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)
{
//不用定位
LogService.Write ("LocationDataGrid(System.Web.UI.Page thePage, DataGrid theDG, string SessionName)");
LogService.Write ("在將DataGrid定位到特定的索引頁上時發生錯誤。");
LogService.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);";
//theDG.Items[i].Attributes["style"] = "cursor:hand";
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 根據不同的刪除方法進行不同的調用,對選中的數據進行刪除(二期老版本)
/// </summary>
/// <param name="thePage">調用此方法的頁面對象</param>
/// <param name="theDG">存儲數據的DataGrid控件對象</param>
/// <param name="sql">刪除之后重新綁定數據時所用的SQL語句,用ViewState["sql"].ToString()作為參數即可</param>
/// <param name="SessionName">用于和服務器進行交互的Session名稱,建議使用"Data"和"Data1"兩者中的一個</param>
/// <param name="CheckBoxName">存儲選中標志的CheckBox控件的ID</param>
/// <param name="DataTypes">主關鍵字列表的數據類型組成的字符型數組,一定要按刪除方法的參數的先后順序排列</param>
/// <param name="Pk">存儲主關鍵字的Hidden控件對象名組成的字符型數組,一定要按刪除方法的參數的先后順序排列</param>
/// <param name="ClassName">定義刪除方法的類名稱(需要給出完整路徑)</param>
/// <param name="MethodName">刪除方法的名稱</param>
/// <param name="PathLevel">出錯頁面與當前頁面的相對路徑關系</param>
/// <param name="ErrorMessage">出錯信息文本</param>
/// <returns>布爾型返回值,成功執行返回true,發生錯誤返回false</returns>
/// **************************************************************************
public static bool DelSelectRecord( System.Web.UI.Page thePage,
System.Web.UI.WebControls.DataGrid theDG,
string sql,
string SessionName,
string CheckBoxName,
string[] DataTypes,
string[] Pk,
string ClassName,
string MethodName,
int PathLevel,
string ErrorMessage)
{
//生成錯誤頁面的路徑
string path = "";
//獲取當前的流程ID(FlowId)
string FlowId = "";
for(int k=0; k<PathLevel; k++)
{
path = path + "../";
}
//打開數據庫連接
SqlConnection SqlCn = new SqlConnection (com.unicafe.common.Configuration.GetDBConnectionString());
SqlCn.Open();
try
{
//開始事務
//SqlTransaction SqlTrans = SqlCn.BeginTransaction();
//聲明存儲主關鍵字的變量
object[] obj = new object[DataTypes.Length];
//對本頁上DataGrid的所有行進行遍歷
for (int i=0; i<theDG.Items.Count; i++)
{
//將當前行賦值給一個DataGridItem對象
DataGridItem _item = theDG.Items[i];
//判斷當前行上的CheckBox控件賦值給一個CheckBox對象
CheckBox CheckFlag = (CheckBox)_item.FindControl(CheckBoxName);
//判斷當前行上的復選框是否被選中,如果被選中則進行刪除處理,否則不予處理
if(CheckFlag.Checked == true)
{
//獲取關鍵字的值,逐個加入對象數組obj
for (int j=0; j<Pk.Length; j++)
{
obj[j] = ((System.Web.UI.HtmlControls.HtmlInputHidden)_item.FindControl(Pk[j].ToString())).Value; //取各項主關鍵字
}
//調用刪除方法進行刪除處理,如果沒有成功刪除則回滾事務并進入錯誤頁面
if(Reflection(ClassName, MethodName, DataTypes, obj, SqlCn) == false)
{
//SqlTrans.Rollback();
//path = path + "publics/Error.aspx?errmsg=" + ErrorMessage;
//定向到錯誤頁面
//thePage.Response.Redirect(path);
//打開錯誤信息提示頁面
Prompt.PromptError(thePage, ErrorMessage);
return false;
}
}
}
//提交數據庫修改
//SqlTrans.Commit();
//重新綁定DataGrid控件
if(CommonService.BindDataGrid(thePage,sql,theDG,SessionName,true) == false)
{
//打開錯誤信息提示頁面
Prompt.PromptError(thePage, "已經成功刪除選定的數據,但在重新綁定數據時發生錯誤。");
return false;
}
else
{
//判斷此記錄是否在某工作流中
FlowId = thePage.Request.QueryString["flowid"];
//LogService.Write("現在要刪除的流程ID:" + FlowId);
if (FlowId != null)
{
WorkflowMgr wMgr = new WorkflowMgr();
wMgr.DelMessage(FlowId);
thePage.Response.Write("<script>");
thePage.Response.Write("window.parent.refreshOpener();");
thePage.Response.Write("window.parent.close();");
thePage.Response.Write("</script>");
}
}
return true;
}
catch(Exception e)
{
//打開錯誤信息提示頁面
Prompt.PromptError(thePage, ErrorMessage);
LogService.Write(e.Message);
return false;
}
finally
{
SqlCn.Close();
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 根據不同的情況調用不同的刪除方法(二期老版本)
/// </summary>
/// <param name="ClassName">定義該刪除方法的類名</param>
/// <param name="MethodName">刪除方法名</param>
/// <param name="DataType">主關鍵字字段數據類型組成的數組</param>
/// <param name="pk">主關鍵字字段組成的數組</param>
/// **************************************************************************
public static bool Reflection(string ClassName, string MethodName, string[] DataTypes, object[] pk, SqlConnection SqlCn)
{
try
{
Assembly[] asm = AppDomain.CurrentDomain.GetAssemblies();
Type t=null;
int i=0;
do
{
t = asm[i].GetType(ClassName);
i++;
} while (t == null && i<asm.Length);
object obj = Activator.CreateInstance(t);
//將傳入的數據類型字符串轉化為數據類型數組
Type[] types = ConvertDataType(SqlCn, DataTypes);
//根據方法名和數據類型數組獲取方法
MethodInfo m = t.GetMethod(MethodName,types);
//根據數據類型轉化數據
object[] aryObj = ConvertData(SqlCn, DataTypes, pk);
//如果轉化成功則調用查找到的方法執行
if (aryObj == null)
{
LogService.Write("public static bool reflection(string ClassName, string MethodName, string[] DataType, object[] pk)");
LogService.Write("對主關鍵字進行特定數據類型轉換的操作不成功。");
return false;
}
else
{
object result = m.Invoke(obj,aryObj);
return (bool)result;
}
}
catch(Exception e)
{
LogService.Write("public static bool Reflection(string ClassName, string MethodName, string[] DataTypes, object[] pk)");
LogService.Write("反射函數執行失敗。ClassName:" + ClassName + "; MethodName:" + MethodName);
LogService.Write(e.Message);
return false;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 將對象數組按照給定的數據類型進行轉換(二期老版本)
/// </summary>
/// <param name="DataType">對象數組對應的數據類型數組</param>
/// <param name="pk">主關鍵字字段組成的數組</param>
/// **************************************************************************
/// BEIGIN
/// <returns>返回轉化后的對象數組</returns>
public static object[] ConvertData(SqlConnection SqlCn, string[] DataType, object[] pk)
{
try
{
object[] obj = new object[DataType.Length + 1];
obj[0] = SqlCn;
for(int i=0; i<DataType.Length; i++)
{
if (DataType[i].ToLower() == "int") //整型
{
obj[i+1] = int.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "double") //雙精度浮點型
{
obj[i+1] = double.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "datetime") //日期型
{
obj[i+1] = System.DateTime.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "float") //單精度浮點型
{
obj[i+1] = float.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "decimal") //十進制小數型
{
obj[i+1] = decimal.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "long") //長整型
{
obj[i+1] = long.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "short") //短整型
{
obj[i+1] = short.Parse(pk[i].ToString());
}
else if(DataType[i].ToLower() == "bool") //布爾型
{
obj[i+1] = bool.Parse(pk[i].ToString());
}
else //不處理
{
obj[i+1] = pk[i].ToString();
}
}
return obj;
}
catch(Exception e)
{
LogService.Write("public static void ConvertDataType(SqlConnection SqlCn, string[] DataType, object[] pk)");
LogService.Write(e.Message);
return null;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 將數據類型名稱數組轉化為數據類型數組(二期老版本)
/// </summary>
/// <param name="DataTypes">數據類型名稱數組</param>
/// <returns></returns>
/// **************************************************************************
public static Type[] ConvertDataType(SqlConnection SqlCn, string[] DataTypes)
{
try
{
Type[] types = new Type[DataTypes.Length + 1];
types[0] = typeof(SqlConnection);
for(int i=0; i<DataTypes.Length; i++)
{
if(DataTypes[i]=="int")
{
types[i+1] = typeof(int);
}
else if(DataTypes[i]=="string")
{
types[i+1] = typeof(string);
}
else if(DataTypes[i]=="float")
{
types[i+1] = typeof(float);
}
else if(DataTypes[i]=="long")
{
types[i+1] = typeof(long);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -