2014-09-01 49 views
-1

我正在開發一個項目(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.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方法

+0

嗨。 BLMethods ctor的最後一行不會引發編譯錯誤嗎? – deostroll 2014-09-01 09:36:04

回答

1

我會以相反的方式做到這一點。該方法添加到類(一個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;} 
} 
+0

當我按照您的方法操作時,出現錯誤,[GridView ID爲'GridView1'的GridView的數據源沒有任何屬性或屬性可以生成列。確保您的數據源包含內容。] – 2014-09-01 09:25:36

+0

@PavanKumar我們可以看到clsPerson類嗎? – 2014-09-01 09:28:12

+0

public class clsPerson { public int personID; public string personName; } – 2014-09-01 09:31:58

0

您必須以其他方式工作。你在BL中編寫方法,返回objPersonList從你的頁面調用它來綁定。

+0

當我跟着你的方法,我得到錯誤,[GridView ID爲'GridView1'的數據源沒有任何屬性或屬性從中生成列。確保您的數據源包含內容。] – 2014-09-01 09:25:52

1

您應該只返回業務規則類中的數據,並在類後面的代碼中綁定網格視圖。

你可以在類的方法,這將返回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(); 
    } 
+0

這裏在BLMethods我需要檢查用戶的授權,如果他是有效的,我需要調用BindGridView或導航到錯誤頁面 – 2014-09-01 09:29:38

+0

檢查自動化爲什麼不使用表單認證 – 2014-09-01 09:31:30

+0

我們使用數據庫進行授權,通過發送用戶名,密碼,pagename到程序,我們得到一個響應對象 – 2014-09-01 09:33:54

1

你應該看看正確分隔你的顧慮。根據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... 
    } 
}