从调用类文件的方法ASP.NET页面的方法 [英] Call ASP.NET page method from class file method

查看:124
本文介绍了从调用类文件的方法ASP.NET页面的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目(ASP.NET网站),我需要调用网页的方法从一个类。

I am working on a project(ASP.NET website) where i need to call method in webpage from a class.

///默认页面方法是

///Default Page Method is

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       BLMethods objBLMethods = new BLMethods();
       objBLMethods.BindingDataToControls();
    }

    public void BindGridView(List<clsPerson> objPersonList)
    {                      
      GridView1.DataSource = objPersonList.ToList();
      GridView1.DataBind();
    }
}

`

类文件结构

    public class BLMethods 
    {
        public BLMethods()
        {
          List<clsPerson> objPersonList = new List<clsPerson>();
          clsPerson objPerson = new clsPerson();
          objPerson.personID = i;
          objPerson.personName = "Person" + i;
          objPersonList.Add(objPerson);
          BindGridView(objPersonList);
        }
    }

clsPerson类:

clsPerson Class:

public class clsPerson 
{ 
    public int personID; 
    public string personName; 
}

如上面的程序中显示,我需要从BLMethods类的构造函数中调用页面BindGridView方法

As shown in the above program, I need to call BindGridView method of page from constructor of BLMethods class

推荐答案

我会做它周围的其他方法。该方法添加到类(带有一个GridView作为参数):

I would do it the other way around. Add the method to the class (with a GridView as an argument):

public class BLMethods 
{
    public BLMethods(GridView gv)
    {
      List<clsPerson> objPersonList = new List<clsPerson>();
      clsPerson objPerson = new clsPerson();
      objPerson.personID = i;
      objPerson.personName = "Person" + i;
      objPersonList.Add(objPerson);
      BindGridView(gv,objPersonList);
    }
    private void BindGridView(GridView gv, List<clsPerson> objPersonList)
    {                      
      gv.DataSource = objPersonList.ToList();
      gv.DataBind();
    }
}

默认页面方法是

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       BLMethods objBLMethods = new BLMethods(GridView1);
       objBLMethods.BindingDataToControls();
    }

}

先给getter和setter您clsPerson类的属性:

Try giving getters and setters to your clsPerson class properties:

public class clsPerson 
{ 
    public int personID {get;set;} 
    public string personName {get;set;}
}

这篇关于从调用类文件的方法ASP.NET页面的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆