创建WEB页面基类,存放所有页面公有方法

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值