Call ASP.NET page method from class file method

前端 未结 4 1801
不思量自难忘°
不思量自难忘° 2021-01-29 05:38

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 :         


        
相关标签:
4条回答
  • 2021-01-29 06:19

    You have to work other way around. You write the method in BL that returns the objPersonList call it from your page to bind.

    0 讨论(0)
  • 2021-01-29 06:23

    You should be returning only data from business rule class and bind grid view in the code behind class.

    you can make method in the class which will return the List<clsPerson> and on page load bind it with your 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 ;
            }
        }
    

    and in code behind of page:

    protected void Page_Load(object sender, EventArgs e)
        {
           BindGridView();
        }
    
        public void BindGridView()
        {             
          BLMethods objBLMethods = new BLMethods();
          GridView1.DataSource = objBLMethods.GetPersons();
          GridView1.DataBind();
        }
    
    0 讨论(0)
  • 2021-01-29 06:24

    You should look to separate your concerns correctly. Based on comments and queries from Ehsan Sajjad's answer:

    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...
        }
    }
    
    0 讨论(0)
  • 2021-01-29 06:29

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

    Default Page Method is

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

    Try giving getters and setters to your clsPerson class properties:

    public class clsPerson 
    { 
        public int personID {get;set;} 
        public string personName {get;set;}
    }
    
    0 讨论(0)
提交回复
热议问题