从类文件方法调用ASP.NET页面方法
我正在开发一个项目(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方法
我会以相反的方式做到这一点。该方法添加到类(一个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;}
}
当我按照您的方法操作时,出现错误,[GridView ID为'GridView1'的GridView的数据源没有任何属性或属性可以生成列。确保您的数据源包含内容。] – 2014-09-01 09:25:36
@PavanKumar我们可以看到clsPerson类吗? – 2014-09-01 09:28:12
public class clsPerson { public int personID; public string personName; } – 2014-09-01 09:31:58
您必须以其他方式工作。你在BL中编写方法,返回objPersonList从你的页面调用它来绑定。
当我跟着你的方法,我得到错误,[GridView ID为'GridView1'的数据源没有任何属性或属性从中生成列。确保您的数据源包含内容。] – 2014-09-01 09:25:52
您应该只返回业务规则类中的数据,并在类后面的代码中绑定网格视图。
你可以在类的方法,这将返回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();
}
这里在BLMethods我需要检查用户的授权,如果他是有效的,我需要调用BindGridView或导航到错误页面 – 2014-09-01 09:29:38
检查自动化为什么不使用表单认证 – 2014-09-01 09:31:30
我们使用数据库进行授权,通过发送用户名,密码,pagename到程序,我们得到一个响应对象 – 2014-09-01 09:33:54
你应该看看正确分隔你的顾虑。根据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...
}
}
嗨。 BLMethods ctor的最后一行不会引发编译错误吗? – deostroll 2014-09-01 09:36:04