📄 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 + -