[英]Call ASP.NET page method from class file method
我正在一個項目(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方法
我會反過來做。 將方法添加到類中(以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();
}
}
嘗試將getter和setter賦予clsPerson類屬性:
public class clsPerson
{
public int personID {get;set;}
public string personName {get;set;}
}
您應該只返回業務規則類的數據,並在類后面的代碼中綁定網格視圖。
您可以在將返回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();
}
您應該設法正確分離您的關注點。 根據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...
}
}
您必須采取其他方法。 您在BL中編寫了返回objPersonList的方法,該方法從頁面進行調用以進行綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.