Gs_Class._BaseQueryWeb查询页面基类(aspx.net)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//
using System.Data.OleDb;

namespace Gs_Class
{
public partial class _BaseQueryWeb : System.Web.UI.Page
{ //查询基类
//全局变量
public string sGlobalError = "";

public virtual void Page_Load(object sender, EventArgs e)
{ //初始化
if (Page.IsPostBack) return;
OleDbCommand cmd = null;
initializeFormView(cmd);
initializeDataView(cmd);
//设置SEO参数
try { setSeoParam(); }
catch (Exception x) { }
}


#region //设置SEO参数 setSeoParam
public virtual bool setSeoParam() { return true; }
#endregion


#region//生成查询条件 buildFilter
/// <summary>
/// 生成查询条件
/// </summary>
/// <param name="sFilter">返回的查询条件</param>
/// <returns></returns>
public virtual bool buildFilter(ref string sFilter)
{
return buildFilter("", ref sFilter);
}
public virtual bool buildFilter(string sAddinFilter, ref string sFilter)
{
sFilter = sAddinFilter;
if (string.IsNullOrEmpty(sFilter)) sFilter = "";
return true;
}
public virtual string buildFilter(string sAddinFilter)
{ //直接返回string
string sF = "";
buildFilter(sAddinFilter, ref sF);
return sF.Trim();
}
/// <summary>
/// 关联查询条件
/// </summary>
/// <param name="sBaseFilter">原来的查询条件</param>
/// <param name="sFilterToAdd">要加到原来条件中的新条件</param>
/// <returns></returns>
public virtual bool linkFilter(ref string sBaseFilter, string sFilterToAdd)
{
return Gs_DataFunction.linkFilter(ref sBaseFilter, sFilterToAdd);
}
#endregion


#region//生成sql查询脚本,buildSqlScript。由runQuery调用
/// <summary>
/// 生成sql查询脚本,由runQuery调用
/// </summary>
/// <param name="sAddinFilter">附加的查询条件</param>
/// <param name="sql">生成的完整脚本</param>
/// <returns></returns>
public virtual bool buildSqlScript(string sAddinFilter, ref string sql)
{ //生成查询脚本
sql = getSqlSelectScriptFullNamed();
string sF = sAddinFilter;
//重要修改:2012-2-20,周承昊。原来查询条件生成不在这里边,拿到里边来生成。这可能引起之前的查询条件出错
sF = buildFilter(sF);
//修改结束:2012-2-20
if (!string.IsNullOrEmpty(sF)) sql += " and (" + sF + ") ";
//
return true;
}
#endregion


#region //脚本定义:加载、写入、删除 getSqlXXXScript
public virtual string getSqlSelectScript(params object[] args) { return "select * from sysObjects "; }
public virtual string getSqlSelectScriptFullNamed(params object[] args) { return getSqlSelectScript(args); }
public virtual string getSqlSelectScriptList(params object[] args) { return "select * from sysObjects "; }
public virtual string getSqlSelectScriptFullNamedList(params object[] args) { return getSqlSelectScriptList(args); }
public virtual string getSqlSaveScript(params object[] args) { return "select * from sysObjects "; }
public virtual string getSqlDeleteScript(params object[] args) { return "select * from sysObjects "; }
public virtual string getSqlPassScript(params object[] args) { return ""; }
#endregion


#region//查询系列,runQuery,可显示、打印、输出excel
/// <summary>
/// 查询系列,runQuery,可显示、打印、输出excel
/// </summary>
/// <param name="sAddinFilter">附加查询条件</param>
/// <param name="lShow">显示,默认显示到grd_Main上</param>
/// <param name="lPrint">打印</param>
/// <param name="sExportToExcelFileName">输出到excel的文件名,没有则忽略</param>
/// <returns>成功与否</returns>
public virtual bool runQuery(string sAddinFilter, bool lShow, bool lPrint, string sExportToExcelFileName, params object[] args)
{ //查询3
bool result = false;
string s = "", sql = "";
//
GridView grd = null;
object lbl = null;
OleDbConnection conn = null;
OleDbDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
BoundField bf = null;
try
{
//开始前的检查
if (!beforeRunQuery(sAddinFilter, lShow, lPrint, sExportToExcelFileName, args)) return false;
//查找控件
string sGrd = defaultGridViewName;
grd = getDefaultGridView();
if (grd == null) throw new Exception(fanyiStr("没有找到控件") + ":" + sGrd);
//设置grid的空显示
if (string.IsNullOrEmpty(grd.EmptyDataText)) grd.EmptyDataText = getDefaultEmptyDataText();
//
lbl = FindControl("lbl_Info");
if (lbl is Label) (lbl as Label).Text = "";
//脚本
if (!buildSqlScript(sAddinFilter, ref sql)) return false;
//数据处理
conn = getNewConnection();
da = new OleDbDataAdapter(sql, conn);
setQueryDataAdapterParam(da);
ds = new DataSet();
da.Fill(ds, "QueryResult");
dt = ds.Tables["QueryResult"];
//查询到数据之后
onRunQueryGetDate(ds, sAddinFilter, lShow, lPrint, sExportToExcelFileName, args);
//设置默认数据外观
if (lShow || lPrint || !string.IsNullOrEmpty(sExportToExcelFileName)) setDefaultFieldView(dt, args);
//显示
if (lShow)
{
setDefaultFieldViewShow(dt, args);
grd.DataSource = dt.DefaultView;
grd.DataBind();
//如果是自动生成列,则自动显示名称
if (grd.AutoGenerateColumns)
foreach (DataControlField col in grd.Columns) if (col is BoundField)
{
bf = col as BoundField;
s = bf.DataField;
if (dt.Columns.Contains(s) && dt.Columns[s].Caption != s) bf.HeaderText = dt.Columns[s].Caption;
}
}
//打印
if (lPrint)
{ setDefaultFieldViewPrint(dt, args); }
//输出excel
if (!string.IsNullOrEmpty(sExportToExcelFileName))
{
setDefaultFieldViewExportExcel(dt, args);
dt = ds.Tables["QueryResult"];
if (!Gs_DataFunction.exportToExcel(sExportToExcelFileName, dt)) throw new Exception(fanyiStr("导出") + "excel" + fanyiStr("文件") + sExportToExcelFileName + fanyiStr("出错") + "!");
}
//查询完成之后,处理一下
afterRunQuery(ds, sAddinFilter, lShow, lPrint, sExportToExcelFileName, args);
onRunQueryEnd(ds, sAddinFilter, lShow, lPrint, sExportToExcelFileName, args);
//完成
result = true;
}
catch (Exception x)
{
if (lbl is Label) (lbl as Label).Text = fanyiStr("查询出错");
s = Gs_Class.getJspTipScriptQuoted(fanyiStr("查询出错") + "\r\n" + x.Message + (enableDebug ? "\r\n" + fanyiStr("错误来源") + ":[runQuery]" : ""));
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", s, true);
}
finally
{ if (conn != null) conn.Close(); }
//
return result;
}


/// <summary>
/// 查询之前的预处理,比如可以检查设置、权限等
/// </summary>
/// <param name="sAddinFilter"></param>
/// <param name="lShow"></param>
/// <param name="lPrint"></param>
/// <param name="sExportToExcelFileName"></param>
/// <param name="args"></param>
/// <returns></returns>
public virtual bool beforeRunQuery(string sAddinFilter, bool lShow, bool lPrint, string sExportToExcelFileName, params object[] args)
{
return true;
}


/// <summary>
/// 设置查询用的数据接口的参数
/// </summary>
/// <param name="db">要设置的数据对象,设置之后,会执行db.fill(ds,"result")的操作</param>
/// <returns>返回成功与否</returns>
public virtual bool setQueryDataAdapterParam(OleDbDataAdapter db, params object[] args)
{ //设置查询参数
bool result = false;
result = true;
return result;
}

/// <summary>
/// 查询:显示?打印?输出到Excel的文件名?
/// </summary>
/// <param name="lShow"></param>
/// <param name="lPrint"></param>
/// <param name="sExportToExcelFileName"></param>
/// <returns></returns>
public virtual bool runQuery(bool lShow, bool lPrint, string sExportToExcelFileName, params object[] args)
{ //查询2,带excel输出
string sF = "";
//重要修改,2012-2-20,周承昊。
//下边这行原来没有屏蔽,2012-2-20号屏蔽了他。因为会导致重复调用buildFilter。
//这可能会引起之前做的一些查询查询条件出错。
//以后生成查询条件,都要在buildFilter里自己生成
//if (!buildFilter(ref sF)) return false;
//修改结束:2012-2-20
return runQuery(sF, lShow, lPrint, sExportToExcelFileName, args);
}
public virtual bool runQuery(bool lShow, bool lPrint, params object[] args) //查询1
{ return runQuery(lShow, lPrint, "", args); }
/// <summary>
/// RunQuery查询获取到数据之后,处理别的之前
/// </summary>
/// <param name="db">数据对象,dataSet</param>
/// <param name="sAddinFilter">runQuery传入的条件</param>
/// <param name="lShow">显示</param>
/// <param name="lPrint">打印</param>
/// <param name="sExportToExcelFileName">数据EXCEL</param>
/// <param name="args">动态参数</param>
/// <returns></returns>
public virtual bool onRunQueryGetDate(object db, string sAddinFilter, bool lShow, bool lPrint, string sExportToExcelFileName, params object[] args)
{
return true;
}
/// <summary>
/// RunQuery完全执行完毕之后
/// </summary>
/// <param name="db">数据对象,DataSet</param>
/// <param name="sAddinFilter">runQuery传入的查询条件</param>
/// <param name="lShow">显示</param>
/// <param name="lPrint">打印</param>
/// <param name="sExportToExcelFileName">输出EXCEL的文件名</param>
/// <param name="args">附加的动态参数</param>
/// <returns></returns>
public virtual bool onRunQueryEnd(object db, string sAddinFilter, bool lShow, bool lPrint, string sExportToExcelFileName, params object[] args)
{ return true; }
public virtual bool afterRunQuery(DataSet ds, string sAddinFilter, bool lShow, bool lPrint, string sExportToExcelFileName, params object[] args)
{ return true; }
#endregion


#region //查询和常用的链接 getNewConnection getNewQueryConnection
public virtual OleDbConnection getNewConnection() { return Gs_DataFunction.getNewConnection(); }
public virtual OleDbConnection getNewQueryConnection() { return getNewConnection(); }
#endregion


#region //默认的设置数据字段外观 setDefaultFieldView、setDefaultFieldViewShow、setDefaultFieldViewPrint、setDefaultFieldViewExportExcel
public virtual bool setDefaultFieldView(DataTable data, params object[] args)
{
return true;
}
public virtual bool setDefaultFieldViewShow(DataTable data, params object[] args)
{
return setDefaultFieldView(data, args);
}
public virtual bool setDefaultFieldViewPrint(DataTable data, params object[] args)
{
return setDefaultFieldView(data, args);
}
public virtual bool setDefaultFieldViewExportExcel(DataTable data, params object[] args)
{
return setDefaultFieldView(data, args);
}
#endregion


#region //获取显示用的表格控件,getDefaultGridView
/// <summary>
/// 查找默认的用于显示的GridView控件,默认名字是grd_Main,在getDefaultGridViewName方法中设置名字
/// </summary>
/// <returns></returns>
public virtual GridView getDefaultGridView()
{
string sGrd = defaultGridViewName;
ContentPlaceHolder cp = null;
GridView grd = Page.FindControl(sGrd) as GridView;
if (grd == null && Master != null)
{ //是否框架页,是的话,循环所有placeholder
grd = Master.FindControl(sGrd) as GridView;
if (grd == null)
foreach (Control obj in Master.Controls)
{ //查找所有子项
grd = obj.FindControl(sGrd) as GridView; //if (obj is ContentPlaceHolder)
if (grd != null) break;
//强行转换成母版页的内容项
cp = obj as ContentPlaceHolder;
if (cp != null) grd = cp.FindControl(sGrd) as GridView;
if (grd != null) break;
}
if (grd == null)
{
cp = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder; //
if (cp != null) grd = cp.FindControl(sGrd) as GridView;
}
}
return grd;
}
/// <summary>
/// 默认gridView的名字,defaultGridViewName,用于查找显示,绑定显示
/// </summary>
/// <returns></returns>
public virtual string getDefaultGridViewName() { return "grd_Main"; }
public string defaultGridViewName { get { return getDefaultGridViewName(); } }
#endregion


#region //执行处理系列 runWork
public virtual bool runWork(OleDbCommand command, bool lTransaction, params object[] args)
{ //处理
bool result = false, runResult = false;
//
OleDbConnection conn = null;
OleDbCommand cmd = command;
Gs_DataFunction.getDataCommand(cmd, ref conn, ref cmd);
OleDbTransaction trans = cmd.Transaction;
bool lConn = conn.State == ConnectionState.Open, lTrans = trans != null;
//
try
{
//检查数据可靠性
//数据准备
if (!lConn) conn.Open();
if (lTransaction && !lTrans) { trans = conn.BeginTransaction(); cmd.Transaction = trans; } //事务
//处理
runResult = runWorkRun(conn, cmd, trans, lTransaction, args);
//完成
if (lTransaction) trans.Commit();
result = runResult;
}
catch (Exception x)
{
if (lTransaction && trans != null) trans.Rollback(); //撤消事务
onErrorRun(null, null, x, "处理出错![runWork]");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", Gs_Class.getJspTipScriptQuoted("处理出错!\r\n" + x.Message + (enableDebug ? "\r\n错误来源:[runWork]" : "")), true);
}
finally
{ if (!lConn) conn.Close(); }
//完成返回
return result;
}
public virtual bool runWorkRun(OleDbConnection conn, OleDbCommand cmd, OleDbTransaction trans, bool lTransaction, params object[] args)
{ //处理数据的执行部分,直接调用。考虑事务、command是否存在之类问题由上级负责
return true;
}
public virtual bool runWork()
{ return runWork(null as OleDbCommand, true); }
#endregion


#region//应用权限 applyPopedom getPopModule getPopUnit之类定义
protected string fPopModule = "";
protected string fPopUnit = "";
protected string fApplication = "";
public virtual string sPopModule { get { return getPopModule(); } set { setPopModule(value); } }
public virtual string sPopUnit { get { return getPopUnit(); } set { setPopUnit(value); } }
public virtual string getPopModule(params object[] args) { return fPopModule; }
public virtual string getPopUnit(params object[] args) { return fPopUnit; }
public virtual void setPopModule(string AModule, params object[] args) { fPopModule = AModule; }
public virtual void setPopUnit(string AUnit, params object[] args) { fPopUnit = AUnit; }
//项目
public virtual string sApplication { get { return getApplication(); } set { setApplication(value); } }
public virtual string getApplication(params object[] args) { return fApplication; }
public virtual void setApplication(string AValue, params object[] args) { fApplication = AValue; }

public virtual bool applyPopedom(string sUserID, string sApplication, string sPopModule, string sPopUnit, params object[] args)
{
return true;
}
public virtual bool applyPopedom(object _iUser_Of_iPublic, OleDbConnection connection, params object[] args)
{
OleDbConnection conn = connection;
if (conn == null) conn = new OleDbConnection(ConfigurationManager.AppSettings["ConnectionString"]);
OleDbCommand cmd = new OleDbCommand("", conn);
return applyPopedom(_iUser_Of_iPublic, cmd, args);
}
public virtual bool applyPopedom(object _iUser_Of_iPublic, OleDbCommand command, params object[] args)
{
bool result = false;
//
OleDbCommand cmd = command;
OleDbConnection conn = null;
if (cmd != null) conn = command.Connection; //else conn = user.dbConnection;
Gs_DataFunction.getDataCommand(cmd, ref conn, ref cmd);
//
bool lConn = conn.State == ConnectionState.Open;
//处理
try
{
Session.Add("CheckPopedomModule", sPopModule); //记录到Session中备用
Session.Add("CheckPopedomUnit", sPopUnit);
//
result = applyPopedomRun(_iUser_Of_iPublic, conn, command, args);
}
catch (Exception x)
{
onErrorRun(null, null, x, "应用权限出错![applyPopedom]");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", Gs_Class.getJspTipScriptQuoted("应用权限出错!\r\n" + x.Message + "\r\n错误来源:[applyPopedom]"), true);
//throw new Exception("[applyPopedom]应用权限出错!\r\n" + x.Message);
}
finally
{ if (!lConn) conn.Close(); }
//
return result;
}
public virtual bool applyPopedomRun(object _iUser_Of_iPublic, OleDbConnection conn, OleDbCommand cmd, params object[] args)
{ //应用权限
return true;
}
#endregion


#region //出错时执行 onErrorRun
/// <summary>
/// 出错时执行
/// </summary>
/// <param name="sender">调用者</param>
/// <param name="x"></param>
/// <param name="sBindInfo"></param>
/// <returns></returns>
public virtual bool onErrorRun(object sender, EventArgs e, Exception x, string sBindInfo, params object[] args)
{ //出错时的执行
string s = sBindInfo + " " + x.Message;
//if (x != null) s += "\r\n" + x.Message;
//
object obj = Page.FindControl("lbl_Info");
if (obj is Label) (obj as Label).Text = s;
else if (obj is TextBox) (obj as TextBox).Text = s;
//完成
return true;
}
#endregion


#region //是否生效调试接口 enableDebug getEnableDebug
public bool enableDebug { get { return getEnableDebug(); } }
public virtual bool getEnableDebug() { return true; }
public virtual string ifDebugShow(string sInfo) { if (enableDebug) return sInfo; else return ""; }
#endregion


#region//取选定的项目 getIdsSelected
public virtual bool getIdsSelected(ref ArrayList sIDArray)
{
string sCheckBoxControlName = "chk_SelectList";
object obj = getDefaultGridView();
if (!(obj is GridView)) return false;
//
GridView grd = obj as GridView;
return getIdsSelected(ref sIDArray, grd, sCheckBoxControlName);
}
public virtual bool getIdsSelected(ref ArrayList sIDArray, GridView grd, string sCheckBoxControlName)
{ //获取check选定的id们
bool result = false;
int i = 0;
string sID = "";
//
GridViewRow row = null;
CheckBox chk = null;
sIDArray.Clear();
for (i = 0; i < grd.Rows.Count; i++)
{
row = grd.Rows[i];
chk = row.FindControl(sCheckBoxControlName) as CheckBox;
if (chk == null) throw new Exception("查找CheckBox出错:找不到[" + sCheckBoxControlName + "]命名的控件。");
if (!chk.Checked) continue;
//
sID = grd.DataKeys[i].Value.ToString().Trim();
sIDArray.Add(sID);
}
result = true;
//
return result;
}
#endregion


#region //默认的grid翻页处理 defaultGridViewOnPageIndexChanging
/// <summary>
/// 默认的grid翻页处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void defaultGridViewOnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (!(sender is GridView)) return;
(sender as GridView).PageIndex = e.NewPageIndex;
if (!runQuery(true, false)) return;
}

protected void defaultGridViewOnRowDataBound(object sender, GridViewRowEventArgs e)
{ //默认的gridRow数据绑定处理
GridView grd = sender as GridView;
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{ //数据行
}
if (e.Row.RowType == DataControlRowType.Footer)
{ //页脚
defaultShowGridFooter(grd, row, nColFooter0, nColFooter1);
}
}
public int nColFooter0 { get { return getColFooter0Index(); } }
public virtual int getColFooter0Index() { return 0; }
public int nColFooter1 { get { return getColFooter1Index(); } }
public virtual int getColFooter1Index() { return nColFooter0 + 1; }
#endregion


#region //默认的显示页脚 defaultShowGridFooter
public virtual bool defaultShowGridFooter(GridView grd, GridViewRow row, int nRecordCountCell, int nPageCountCell)
{ //显示页脚
bool result = false;
if (!(grd is GridView)) return result;
//
int nRecCount = -1, nPageCount = -1;
DataView dv = null;
DataTable dt = null;
OleDbDataReader dr = null;
//区分取什么类型
object gds = grd.DataSource;
if ((gds is DataView) && (dv = gds as DataView) != null)
nRecCount = dv.Count;
else
if ((gds is DataTable) && (dt = gds as DataTable) != null) nRecCount = dt.Rows.Count;
//显示
string sHangTitle = fanyiStr("行数") + ":", sYeTitle = fanyiStr("页数") + ":";
if (nPageCountCell == nRecordCountCell && nRecordCountCell > -1)
{ //显示在同一格里
row.Cells[nPageCountCell].Text = sHangTitle + nRecCount.ToString() + ";" + sYeTitle + grd.PageCount.ToString();
}
else
{ //显示在不同格里
if (nRecordCountCell > -1) //((row.Cells.Count - 1) >= nRecordCountCell)
row.Cells[nRecordCountCell].Text = sHangTitle + "" + nRecCount.ToString();
if (nPageCountCell > -1) //((row.Cells.Count - 1) >= nPageCountCell)
row.Cells[nPageCountCell].Text = sYeTitle + "" + grd.PageCount.ToString();
}
result = true;
//
return result;
}
#endregion


#region //初始化界面 initializeFormView、initializeDataView。
public virtual bool initializeFormView(OleDbCommand command, params object[] args)
{ //初始化界面
return true;
}
public virtual bool initializeDataView(OleDbCommand command, params object[] args)
{ //初始化数据
return true;
}
#endregion


#region //获取当前路径相访问root的相对路径 GetRootRelativePath
/// <summary>
/// 获取当前路径相访问root的相对路径,传入当前地址,传入空则默认为“。”,当前路径。
/// 返回这种格式:../../,然后程序可以这样用:../scripts/jPublic.js
/// </summary>
public virtual string getRootRelativePath()
{
return Gs_Class.getRootRelativeUrl("");
}
#endregion


#region //传入的URL参数,比如A.aspx?P1=1&P2=2&P3=3
/// <summary>
/// 传入的URL参数,比如A.aspx?P1=1&P2=2&P3=3
/// </summary>
/// <returns>Request.QueryString.ToString()</returns>
public virtual string get传入的全部参数URL字符串()
{
return HttpContext.Current.Request.QueryString.ToString();
}
#endregion


#region //消息提示 jsAlert
public static void jsAlert(Control obj, string str)
{
ScriptManager.RegisterStartupScript(obj, obj.GetType(), "js0044" + DateTime.Now.ToString("hhmmss"), Gs_Class.getJspTipScript(str), false);
}
#endregion

public virtual string fanyiStr(string sChina) { return fanyiStr(sChina, sChina); }
public virtual string fanyiStr(string sChina, string sDefault) { if (!string.IsNullOrEmpty(sChina)) return sChina; else return sDefault; }
public virtual string getDefaultEmptyDataText() { return fanyiStr("没有符合查询要求的数据"); }
//public virtual string get行数Title() { return fanyiStr("行数"); }
//public virtual string get页数Title() { return fanyiStr("页数"); }
public static object tryNullTo(object oValue, object oDefault) { return Gs_Class.tryNullTo(oValue, oDefault); }


}

}

posted @ 2016-02-25 16:22  海宏软件  阅读(279)  评论(0编辑  收藏  举报