我是在使用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;
}
}
... {
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;
}
}*/
... {
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;
}
}*/
... {
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;
}
}*/