Call ASP.NET page method from class file method

前端 未结 4 1808
不思量自难忘°
不思量自难忘° 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: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 objPersonList = new List();
          clsPerson objPerson = new clsPerson();
          objPerson.personID = i;
          objPerson.personName = "Person" + i;
          objPersonList.Add(objPerson);
          BindGridView(gv,objPersonList);
        }
        private void BindGridView(GridView gv, List 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;}
    }
    

提交回复
热议问题