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

C# 从类文件方法调用ASP.NET页面方法,c#,asp.net,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.BindingDataToCon

我正在做一个项目(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 partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       BLMethods objBLMethods = new BLMethods(GridView1);
       objBLMethods.BindingDataToControls();
    }

}

如上面的程序所示,我需要从BLMethods类的构造函数中调用page的BindGridView方法,您必须以其他方式进行操作。您可以在BL中编写返回objPersonList的方法,并从要绑定的页面中调用它。

我可以用另一种方法。将方法添加到类(以GridView作为参数):

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

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

您应该只从代码隐藏类中的业务规则类和绑定网格视图返回数据

您可以在类中创建方法,该方法将返回
列表
,并在页面加载时将其与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 ;
        }
    }

你应该注意正确区分你的顾虑。根据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...
    }
}
public分部类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!new AuthenticationHelper().isUserAuthorizedForPeople(Request.User.Identity))
{
Response.Redirect(“顽皮无能.aspx”);
}
BindGridView();
}
public void BindGridView()
{                      
PersonHelper=新的PersonHelper();
GridView1.DataSource=helper.GetPeople();
GridView1.DataBind();
}
}
公共类AuthenticationHelper()
{
公共bool IsUserAuthorizedForPeople(字符串用户名){
返回true;//在此处进行身份验证。
}
}
公共类PersonHelper
{
私人有限公司
{                      
List objPersonList=新列表();
//填充您的人员列表。
返回objPersonList;
//顺便说一句,匈牙利的命名符号会让你
//代码看起来很混乱。。。
}
}

当我遵循您的方法时,我得到了一个错误,[id为'GridView1'的GridView数据源没有任何属性或属性可用于生成列。请确保您的数据源具有内容。]@PavanKumar我们可以看到clsPerson类吗?公共类clsPerson{public int personID;public string personName;}当我采用您的方法时,我得到的错误是,[id为'GridView1'的GridView的数据源没有用于生成列的任何属性或属性。请确保您的数据源具有内容。]在BLMethods中,我需要检查用户的授权,如果他是有效的,我需要调用BindGridView或导航到错误页面检查自动化为什么不使用表单身份验证我们正在使用数据库进行授权,通过向过程发送用户名、密码、页面名,我们会得到响应object@PavanKumar-你们班在做too很多东西。有一个单独的类来处理身份验证,并根据那里的结果执行检查和重定向。然后让你的person类根据用户返回数据。不要试图将前端代码拉入后端。@Paddy。你解释的概念我不清楚。你能用abov解释我吗我提到的e代码。嗨。BLCTOR方法中的最后一行不会抛出编译错误吗?
public class clsPerson 
{ 
    public int personID {get;set;} 
    public string personName {get;set;}
}
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();
    }
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...
    }
}