简体   繁体   中英

Call ASP.NET page method from class file method

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

`

class file structure is

    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 Class:

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

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

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

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

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...
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM