您的位置:首页 > 其它

创建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;


}


}*/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: