asp:表示层的 dropdownlist 怎么接受数据访问层传回来的泛型填充到dropdownlist 中去。
asp:表示层的dropdownlist怎么接受数据访问层传回来的泛型填充到dropdownlist中去。...
asp:表示层的 dropdownlist 怎么接受数据访问层传回来的泛型填充到dropdownlist 中去。
展开
1个回答
展开全部
就以你的事列为准,前台我假如有两个下拉框(DropDownList),一个班级(Dl_Grade),另一个为学生(Dl_Student)。
要求:当用户班级下拉框发生改变的时候,学生下拉框也随之改变。(采用泛型数据)
实现:
1。构建泛型类
2。填充泛型数据源
3。绑定下拉框
4。实现Chang事件
为了你这分,刚才写了下:
泛型类:
/// summary
/// 定义一个年级学生类,便于泛型使用
/// /summary
public class GradeStudent
{
//年级
private string grade;
public string Grade
{
get { return grade; }
set { grade = value; }
}
//学生(姓名)
private string student;
public string Student
{
get { return student; }
set { student = value; }
}
public GradeStudent(string _grade, string _student)
{
this.grade = _grade;
this.student = _student;
}
}
填充数据源
/// summary
/// 获取数据源
/// /summary
/// returns/returns
public ListGradeStudent GetList()
{
ListGradeStudent _GradeStudent = new ListGradeStudent();
_GradeStudent.Add(new GradeStudent("一年级", "张三"));
_GradeStudent.Add(new GradeStudent("一年级", "李四"));
_GradeStudent.Add(new GradeStudent("一年级", "王五"));
_GradeStudent.Add(new GradeStudent("二年级", "李秀"));
_GradeStudent.Add(new GradeStudent("二年级", "余娟"));
_GradeStudent.Add(new GradeStudent("二年级", "刘丽丽"));
_GradeStudent.Add(new GradeStudent("二年级", "胡小平"));
_GradeStudent.Add(new GradeStudent("三年级", "我一人"));
_GradeStudent.Add(new GradeStudent("四年级", "小强"));
_GradeStudent.Add(new GradeStudent("四年级", "黑娃"));
_GradeStudent.Add(new GradeStudent("四年级", "小白"));
_GradeStudent.Add(new GradeStudent("四年级", "吴铭志"));
_GradeStudent.Add(new GradeStudent("四年级", "随便吹"));
return _GradeStudent;
}
填充下拉框
/// summary
/// 绑定年级下拉框
/// /summary
private void SetGrade()
{
//设置班级
foreach (GradeStudent grade in GetList())
{
if (IsOk(grade.Grade))
Gr.Add(grade.Grade);
}
Dl_Grade.DataSource = Gr;
Dl_Grade.DataBind();
}
/// summary
/// 绑定学生下拉框
/// /summary
private void SetStudent()
{
//设置姓名下拉框
Gr.Clear();
foreach (GradeStudent grade in GetList())
{
if (grade.Grade == Dl_Grade.SelectedValue)
{
Gr.Add(grade.Student);
}
}
Dl_Student.DataSource = Gr;
Dl_Student.DataBind();
}
用到判断方法:
/// summary
/// 年级重复筛选
/// /summary
/// param name="s"/param
/// returns/returns
private bool IsOk(string s)
{
foreach (string Str in Gr)
{
if (Str == s)
return false;
}
return true;
}
初始时:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//初始
SetGrade();
SetStudent();
}
}
注:全局泛型变量
//定义全局泛型变量,用来做数据源
Liststring Gr = new Liststring();
年级下拉框事件
//动态变化事件
protected void Dl_Grade_SelectedIndexChanged(object sender, EventArgs e)
{
SetStudent();
}
要求:当用户班级下拉框发生改变的时候,学生下拉框也随之改变。(采用泛型数据)
实现:
1。构建泛型类
2。填充泛型数据源
3。绑定下拉框
4。实现Chang事件
为了你这分,刚才写了下:
泛型类:
/// summary
/// 定义一个年级学生类,便于泛型使用
/// /summary
public class GradeStudent
{
//年级
private string grade;
public string Grade
{
get { return grade; }
set { grade = value; }
}
//学生(姓名)
private string student;
public string Student
{
get { return student; }
set { student = value; }
}
public GradeStudent(string _grade, string _student)
{
this.grade = _grade;
this.student = _student;
}
}
填充数据源
/// summary
/// 获取数据源
/// /summary
/// returns/returns
public ListGradeStudent GetList()
{
ListGradeStudent _GradeStudent = new ListGradeStudent();
_GradeStudent.Add(new GradeStudent("一年级", "张三"));
_GradeStudent.Add(new GradeStudent("一年级", "李四"));
_GradeStudent.Add(new GradeStudent("一年级", "王五"));
_GradeStudent.Add(new GradeStudent("二年级", "李秀"));
_GradeStudent.Add(new GradeStudent("二年级", "余娟"));
_GradeStudent.Add(new GradeStudent("二年级", "刘丽丽"));
_GradeStudent.Add(new GradeStudent("二年级", "胡小平"));
_GradeStudent.Add(new GradeStudent("三年级", "我一人"));
_GradeStudent.Add(new GradeStudent("四年级", "小强"));
_GradeStudent.Add(new GradeStudent("四年级", "黑娃"));
_GradeStudent.Add(new GradeStudent("四年级", "小白"));
_GradeStudent.Add(new GradeStudent("四年级", "吴铭志"));
_GradeStudent.Add(new GradeStudent("四年级", "随便吹"));
return _GradeStudent;
}
填充下拉框
/// summary
/// 绑定年级下拉框
/// /summary
private void SetGrade()
{
//设置班级
foreach (GradeStudent grade in GetList())
{
if (IsOk(grade.Grade))
Gr.Add(grade.Grade);
}
Dl_Grade.DataSource = Gr;
Dl_Grade.DataBind();
}
/// summary
/// 绑定学生下拉框
/// /summary
private void SetStudent()
{
//设置姓名下拉框
Gr.Clear();
foreach (GradeStudent grade in GetList())
{
if (grade.Grade == Dl_Grade.SelectedValue)
{
Gr.Add(grade.Student);
}
}
Dl_Student.DataSource = Gr;
Dl_Student.DataBind();
}
用到判断方法:
/// summary
/// 年级重复筛选
/// /summary
/// param name="s"/param
/// returns/returns
private bool IsOk(string s)
{
foreach (string Str in Gr)
{
if (Str == s)
return false;
}
return true;
}
初始时:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//初始
SetGrade();
SetStudent();
}
}
注:全局泛型变量
//定义全局泛型变量,用来做数据源
Liststring Gr = new Liststring();
年级下拉框事件
//动态变化事件
protected void Dl_Grade_SelectedIndexChanged(object sender, EventArgs e)
{
SetStudent();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询