ASP.NET―001:GridView绑定List、页面返回值具体实现
本文导语: 用惯了WPF的绑定,ASP.NET的绑定貌似不是很好用。下面看看ASP.NET绑定的用法。一般来说可以直接绑定DataTable的,不过我觉得绑定List比较符合面向对象编程。绑定的方法是两句代码: 代码如下:GridView名.DataSource = List; GridView名.Da...
用惯了WPF的绑定,ASP.NET的绑定貌似不是很好用。下面看看ASP.NET绑定的用法。一般来说可以直接绑定DataTable的,不过我觉得绑定List比较符合面向对象编程。
绑定的方法是两句代码:
GridView名.DataSource = List;
GridView名.DataBind();
直接看例子吧,以下是一个绑定一个PersonModel类的例子。其中用到了页面返回参数,使用js传递,js可写在前端也可直接写在后台代码里。
项目结构:
效果:
实体类
public class PersonModel
{
private int personIndex;
public int PersonIndex
{
get { return personIndex; }
set { personIndex = value; }
}
private string personID;
public string PersonID
{
get { return personID; }
set { personID = value; }
}
private string personName;
public string PersonName
{
get { return personName; }
set { personName = value; }
}
private string personSex;
public string PersonSex
{
get { return personSex; }
set { personSex = value; }
}
private int personAge;
public int PersonAge
{
get { return personAge; }
set { personAge = value; }
}
private bool personSelected = false;
public bool PersonSelected
{
get { return personSelected; }
set { personSelected = value; }
}
}
针对绑定的aspx页面写一个管理类,用于操作数据
public class ChildFrmManager
{
private List personCollect = new List();
private static ChildFrmManager instance = null;
public List PersonCollect
{
get { return personCollect; }
set { personCollect = value; }
}
public static ChildFrmManager DoGetInstance()
{
if (instance == null)
{
instance = new ChildFrmManager();
}
return instance;
}
public void DoAddPersons()
{
for (int i = 0; i < 20; i++)
{
PersonModel model = new PersonModel();
model.PersonIndex = i + 1;
model.PersonID = System.Guid.NewGuid().ToString();
model.PersonName = "测试" + i;
model.PersonAge = 27 + i;
model.PersonSex = i % 2 == 0 ? "男" : "女";
model.PersonSelected = false;
this.PersonCollect.Add(model);
}
}
}
绑定的页面前端