创建WEB页面基类,存放所有页面公有方法
2008-03-28 11:26
211 查看
我是在使用ajax时碰到此问题,每个web页面都需要用到ajax函数,并且函数相似,需要把相同的函数copy到每个页面类中,很繁琐,于是就创建了BaseWebPage代码如下:
public class BaseWebPage : System.Web.UI.Page
...{
public BaseWebPage()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
[AjaxPro.AjaxMethod]
public int GetCellFile(int TypeID, string TTUDesc, string para)
...{
try
...{
string selectedTTU = TTUDesc;
if (selectedTTU == "")
...{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
...{
Page.Session["SELECTTU"] = selectedTTU;
}
string TTUNO = CommonClass.GetAllTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
if (CellDegelte.SendMessage(cllFilePath, true, TypeID, para))
...{
return 1;
}
return -3;
}
catch (System.NullReferenceException eer)
...{
string error = eer.Message;
return -2;
}
catch (Exception er)
...{
string error = er.Message;
return -1;
}
}
[AjaxPro.AjaxMethod]
public bool GetSearchResult(string searchflag)
...{
try
...{
bool Ret = CellDegelte.GetResult(searchflag);
return Ret;
}
catch (Exception er)
...{
string error = er.Message;
return false;
}
}
[AjaxPro.AjaxMethod]
public bool DeleteFile(string filepath)
...{
try
...{
FileInfo fi = new FileInfo(Server.MapPath(filepath).ToString());
fi.Delete();
return true;
}
catch (Exception er)
...{
string error = er.Message;
return false;
}
}
这样在我创建页面时,修改页面类继承自BaseWebPage 如下代码:
public partial class PGDKKL : BaseWebPage
...{
protected void Page_Load(object sender, EventArgs e)
...{
AjaxPro.Utility.RegisterTypeForAjax(typeof(PGDKKL));
Page.Session["title"] = "供电可靠率";
}
/**//*
[AjaxPro.AjaxMethod]
public int GetCellFile(int TypeID, string TTUDesc, string para)
{
try
{
string selectedTTU = TTUDesc;
if (selectedTTU == "")
{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
{
Page.Session["SELECTTU"] = selectedTTU;
}
string TTUNO = CommonClass.GetAllTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
//string cllFilePath = "F:/P5WEB/P5WEB/file/hmi-rpt.cll";
if (!ConnectP5.libcll_start())
{
// MessageBox.Show("调用libcll_start返回失败");
return -1;
}
if (!ConnectP5.libcll_setfilepath(cllFilePath, true))
{
//MessageBox.Show("设置报表文件路径失败");
return -1;
}
int taskID = ConnectP5.libcll_gencll(TypeID, para);
return taskID;
}
catch (System.NullReferenceException eer)
{
string error = eer.Message;
return -2;
}
catch (Exception er)
{
string error = er.Message;
return -1;
}
}
[AjaxPro.AjaxMethod]
public int GetCellFile(int TypeID, string TTUDesc, string para)
{
try
{
string selectedTTU = TTUDesc;
if (selectedTTU == "")
{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
{
Page.Session["SELECTTU"] = selectedTTU;
}
string TTUNO = CommonClass.GetAllTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
if (CellDegelte.SendMessage(cllFilePath, true, TypeID, para))
{
return 1;
}
return -3;
}
catch (System.NullReferenceException eer)
{
string error = eer.Message;
return -2;
}
catch (Exception er)
{
string error = er.Message;
return -1;
}
}
[AjaxPro.AjaxMethod]
public bool GetSearchResult(string searchflag)
{
try
{
bool Ret = CellDegelte.GetResult(searchflag);
return Ret;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}
[AjaxPro.AjaxMethod]
public bool DeleteFile(string filepath)
{
try
{
FileInfo fi = new FileInfo(Server.MapPath(filepath).ToString());
fi.Delete();
return true;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}*/
如果碰到不同的就重写对应函数 如下:
public partial class VoltageHGL : BaseWebPage
...{
protected void Page_Load(object sender, EventArgs e)
...{
AjaxPro.Utility.RegisterTypeForAjax(typeof(VoltageHGL));
Page.Session["title"] = "电压合格率";
}
[AjaxPro.AjaxMethod]
public new int GetCellFile(int TypeID, string TTUDesc, string para)
...{
try
...{
string TTUNO;
string selectedTTU = TTUDesc;
if (selectedTTU == "")
...{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
...{
Page.Session["SELECTTU"] = selectedTTU;
}
TTUNO = CommonClass.GetVoltageTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
if (CellDegelte.SendMessage(cllFilePath, true, TypeID, para))
...{
return 1;
}
return -3;
}
catch (System.NullReferenceException eer)
...{
string error = eer.Message;
return -2;
}
catch (Exception er)
...{
string error = er.Message;
return -1;
}
}
/**//*
[AjaxPro.AjaxMethod]
public bool GetSearchResult(string searchflag)
{
try
{
bool Ret = CellDegelte.GetResult(searchflag);
return Ret;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}
[AjaxPro.AjaxMethod]
public bool DeleteFile(string filepath)
{
try
{
FileInfo fi = new FileInfo(Server.MapPath(filepath).ToString());
fi.Delete();
return true;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}*/
public class BaseWebPage : System.Web.UI.Page
...{
public BaseWebPage()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
[AjaxPro.AjaxMethod]
public int GetCellFile(int TypeID, string TTUDesc, string para)
...{
try
...{
string selectedTTU = TTUDesc;
if (selectedTTU == "")
...{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
...{
Page.Session["SELECTTU"] = selectedTTU;
}
string TTUNO = CommonClass.GetAllTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
if (CellDegelte.SendMessage(cllFilePath, true, TypeID, para))
...{
return 1;
}
return -3;
}
catch (System.NullReferenceException eer)
...{
string error = eer.Message;
return -2;
}
catch (Exception er)
...{
string error = er.Message;
return -1;
}
}
[AjaxPro.AjaxMethod]
public bool GetSearchResult(string searchflag)
...{
try
...{
bool Ret = CellDegelte.GetResult(searchflag);
return Ret;
}
catch (Exception er)
...{
string error = er.Message;
return false;
}
}
[AjaxPro.AjaxMethod]
public bool DeleteFile(string filepath)
...{
try
...{
FileInfo fi = new FileInfo(Server.MapPath(filepath).ToString());
fi.Delete();
return true;
}
catch (Exception er)
...{
string error = er.Message;
return false;
}
}
这样在我创建页面时,修改页面类继承自BaseWebPage 如下代码:
public partial class PGDKKL : BaseWebPage
...{
protected void Page_Load(object sender, EventArgs e)
...{
AjaxPro.Utility.RegisterTypeForAjax(typeof(PGDKKL));
Page.Session["title"] = "供电可靠率";
}
/**//*
[AjaxPro.AjaxMethod]
public int GetCellFile(int TypeID, string TTUDesc, string para)
{
try
{
string selectedTTU = TTUDesc;
if (selectedTTU == "")
{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
{
Page.Session["SELECTTU"] = selectedTTU;
}
string TTUNO = CommonClass.GetAllTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
//string cllFilePath = "F:/P5WEB/P5WEB/file/hmi-rpt.cll";
if (!ConnectP5.libcll_start())
{
// MessageBox.Show("调用libcll_start返回失败");
return -1;
}
if (!ConnectP5.libcll_setfilepath(cllFilePath, true))
{
//MessageBox.Show("设置报表文件路径失败");
return -1;
}
int taskID = ConnectP5.libcll_gencll(TypeID, para);
return taskID;
}
catch (System.NullReferenceException eer)
{
string error = eer.Message;
return -2;
}
catch (Exception er)
{
string error = er.Message;
return -1;
}
}
[AjaxPro.AjaxMethod]
public int GetCellFile(int TypeID, string TTUDesc, string para)
{
try
{
string selectedTTU = TTUDesc;
if (selectedTTU == "")
{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
{
Page.Session["SELECTTU"] = selectedTTU;
}
string TTUNO = CommonClass.GetAllTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
if (CellDegelte.SendMessage(cllFilePath, true, TypeID, para))
{
return 1;
}
return -3;
}
catch (System.NullReferenceException eer)
{
string error = eer.Message;
return -2;
}
catch (Exception er)
{
string error = er.Message;
return -1;
}
}
[AjaxPro.AjaxMethod]
public bool GetSearchResult(string searchflag)
{
try
{
bool Ret = CellDegelte.GetResult(searchflag);
return Ret;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}
[AjaxPro.AjaxMethod]
public bool DeleteFile(string filepath)
{
try
{
FileInfo fi = new FileInfo(Server.MapPath(filepath).ToString());
fi.Delete();
return true;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}*/
如果碰到不同的就重写对应函数 如下:
public partial class VoltageHGL : BaseWebPage
...{
protected void Page_Load(object sender, EventArgs e)
...{
AjaxPro.Utility.RegisterTypeForAjax(typeof(VoltageHGL));
Page.Session["title"] = "电压合格率";
}
[AjaxPro.AjaxMethod]
public new int GetCellFile(int TypeID, string TTUDesc, string para)
...{
try
...{
string TTUNO;
string selectedTTU = TTUDesc;
if (selectedTTU == "")
...{
selectedTTU = Page.Session["SELECTTU"].ToString();
}
else
...{
Page.Session["SELECTTU"] = selectedTTU;
}
TTUNO = CommonClass.GetVoltageTTUString(selectedTTU);
para = TTUNO + para;
string cllFilePath = CommonClass.CELL_FILE_PATH1;
if (CellDegelte.SendMessage(cllFilePath, true, TypeID, para))
...{
return 1;
}
return -3;
}
catch (System.NullReferenceException eer)
...{
string error = eer.Message;
return -2;
}
catch (Exception er)
...{
string error = er.Message;
return -1;
}
}
/**//*
[AjaxPro.AjaxMethod]
public bool GetSearchResult(string searchflag)
{
try
{
bool Ret = CellDegelte.GetResult(searchflag);
return Ret;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}
[AjaxPro.AjaxMethod]
public bool DeleteFile(string filepath)
{
try
{
FileInfo fi = new FileInfo(Server.MapPath(filepath).ToString());
fi.Delete();
return true;
}
catch (Exception er)
{
string error = er.Message;
return false;
}
}*/
相关文章推荐
- C#--第七周实验--任务1--创建物体类PhysicalObject,通过其私有字段来存放重量和体积,通过公有方法判断该物体是否会在水中下沉
- mui.init()方法中可以有子页面和预加载两项内容,它们是否都在页面加载完成时创建其对应的webview对象?
- WEB程序打包的详细方法,并自动创建数据库
- 调用js代码获取webView页面中的所有图片及点击事件
- 使用angular-cli webpack创建多个包的方法
- Web页面输出Excel的数字问题解决方法
- [Web]防止用户复制页面内容和另存页面的方法
- 安装Microsoft .NET Framework 4.0后站点所有ASPX页面404解决方法
- asp.net多选项卡页面的创建及使用方法
- Web页面中遍历TextBox(或其它控件)的方法
- Asp.net如何在Web页面上直接打开、编辑、创建Office文档
- WEB页面导出为Word文档后分页&横向打印的方法
- [JS]WEB页面导出为EXCEL文档的方法--转
- WEB页面打印方法,包括如何设置打印分页
- Web项目中创建简单的错误处理页面
- 打印Web页面局部内容的两种常用方法
- 请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?(webform&winform)
- Asp.net WEB页面直接导出Excel方法
- 继承System.Web.UI.Page的页面基类
- 从web页面启动winform程序的实现方法