从类文件方法调用ASP.NET页面方法

时间:2014-09-01 09:11:01

标签: c# asp.net

我正在开发一个项目(ASP.NET网站),我需要从一个类中调用网页中的方法。

///默认页面方法是

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课程:

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

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

4 个答案:

答案 0 :(得分:1)

我会反过来做。将方法添加到类中(使用GridView作为参数):

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();
    }

}

尝试为你的clsPerson类属性提供getter和setter:

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

答案 1 :(得分:1)

您应该只返回业务规则类中的数据,并在类后面的代码中绑定网格视图。

你可以在类中创建将返回List<clsPerson>的方法,并在页面加载时将它与你的girdview绑定:

public class BLMethods 
    {
        public BLMethods()
        {

        }

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

          return objPersonList ;
        }
    }

在页面后面的代码中:

protected void Page_Load(object sender, EventArgs e)
    {
       BindGridView();
    }

    public void BindGridView()
    {             
      BLMethods objBLMethods = new BLMethods();
      GridView1.DataSource = objBLMethods.GetPersons();
      GridView1.DataBind();
    }

答案 2 :(得分:1)

您应该正确地分开您的疑虑。基于Ehsan Sajjad回答的评论和询问:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!new AuthenticationHelper().IsUserAuthorisedForPeople(Request.User.Identity))
       {
           Response.Redirect("NaughtyNaughty.aspx");
       }

       BindGridView();
    }

    public void BindGridView()
    {                      
      PersonHelper helper = new PersonHelper();
      GridView1.DataSource = helper.GetPeople();
      GridView1.DataBind();
    }
}

public class AuthenticationHelper()
{
     public bool IsUserAuthorisedForPeople(string userName) {
        return true; //Do your auth here.
     }
}

public class PersonHelper
{

    private void GetPeople()
    {                      
      List<clsPerson> objPersonList = new List<clsPerson>();

      //Populate your list of people.

      return objPersonList; 
      //BTW - hungarian notation for your naming is just going to make your 
      //code look cluttered...
    }
}

答案 3 :(得分:0)

你必须以其他方式工作。你在BL中编写方法,返回objPersonList从你的页面调用它来绑定。