public static List<Student> Students = new List<Student>();
public static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
Students.Add(new Student("张三", "男", "11"));
Students.Add(new Student("李四", "男", "22"));
Students.Add(new Student("王五", "女", "33"));
DataBind();
}
}
public class Student
{
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
private string sex;
public string Sex
{
get { return this.name; }
set { this.sex = value; }
}
private int age;
public int Age
{
get { return this.age; }
set { this.age = value; }
}
}
protected void btnPrev_Click(object sender, EventArgs e)
{
if (i > 0)
{
i--;
DataBind();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
if (i < Students.Count - 1)
{
i++;
DataBind();
}
}
DataBind();方法是怎么写的呢
Student类改为:
public class Student
{
public Student(string _name,string _sex,int _age)
{
this.name=_name;
this.sex=_sex;
this.age=_age;
}
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
private string sex;
public string Sex
{
get { return this.name; }
set { this.sex = value; }
}
private int age;
public int Age
{
get { return this.age; }
set { this.age = value; }
}
}
或把Page_Load内代码改为:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
Students.Add(new Student(){Name="张三", Sex="男",Age= 11});
Students.Add(new Student(){Name="李四", Sex="男",Age= 22});
Students.Add(new Student(){Name="王五", Sex="女",Age=33});
DataBind();
}
}
@artwl: 为什么又会出现 不包含采用“0”参数的构造函数 呢? 高手,求解!
@彼耶: 哥们儿,上面两段代码只改一处,是“或”
你的 Students.Add(new Student("张三", "男", "11")); 很明显的表明你没有这个构造函数
Student类里面必须有一个
public Student(){}
的构造方法。