1)新建web应用程序
2)添加类ClassDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication9
{
public class ClassDemo
{
public ClassDemo(int _classID, string _className)
{
this.classID = _classID;
this.className = _className;
}
private int classID;//班级编号
public int ClassID
{
get { return classID; }
set { classID = value; }
}
private string className;
public string ClassName//班级名称
{
get { return className; }
set { className = value; }
}
}
}
3)添加类StudentDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication9
{
public class StudentDemo
{
public StudentDemo(int _studentID, string _studentName, int _classID)
{
this.studentID = _studentID;
this.studentName = _studentName;
this.classID = _classID;
}
private int studentID;//学生编号
public int StudentID
{
get { return studentID; }
set { studentID = value; }
}
private string studentName;//学生姓名
public string StudentName
{
get { return studentName; }
set { studentName = value; }
}
private int classID;//班级编号
public int ClassID
{
get { return classID; }
set { classID = value; }
}
}
}
4)添加web窗体Default.aspx
1.前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication9.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlstClass" runat="server" onitemdatabound="dlstClass_ItemDataBound">
<HeaderTemplate>
<tr>
<td>
编号
</td>
<td>
班级
</td>
</tr>
<td colspan="2">—————————————————————————</td>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblClassId" runat="server" Text='<%#Eval("ClassId") %>'></asp:Label>
</td>
<td>
<%#Eval("ClassName") %>
</td>
</tr>
<tr>
<td colspan="2">--------------------------------------------------------------------------</td>
</tr>
<tr>
<td>
<asp:DataList ID="dlstStudent" runat="server">
<ItemTemplate>
<tr>
<td>
<%#Eval("StudentId") %>
</td>
<td>
<%#Eval("StudentName") %>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td colspan="2">--------------------------------------------------------------------------</td>
</tr>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
2.后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Default : System.Web.UI.Page
{
private List<StudentDemo> allStudents;//所有学生
private List<ClassDemo> allClasses;//所有的班级
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SettingAllClasses();
SettingAllStudents();
BindClass();
}
}
/// <summary>
/// 绑定所有班级
/// </summary>
private void BindClass()
{
dlstClass.DataSource = allClasses;
dlstClass.DataBind();
}
/// <summary>
/// 根据班级id获取学生信息
/// </summary>
/// <param name="classId"></param>
/// <returns></returns>
private IEnumerable<StudentDemo> GetStudentByClassId(int classId)
{
IEnumerable<StudentDemo> items = null;
items = allStudents.Where(u => u.ClassID == classId);
return items;
}
/// <summary>
/// 绑定学生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dlstClass_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblClassId = (Label)e.Item.FindControl("lblClassId");
int classId = Convert.ToInt32(lblClassId.Text);
DataList dlstStudent = (DataList)e.Item.FindControl("dlstStudent");
dlstStudent.DataSource = GetStudentByClassId(classId);
dlstStudent.DataBind();
}
}
/// <summary>
/// 设置所有学生
/// </summary>
private void SettingAllStudents()
{
allStudents = new List<StudentDemo>();
for (int i = 1; i <= 9; i++)//一共有9个学生,i表示学生的编号
{
int j = 0;//学生所在编辑的编号
int k = 3;//每个班有3个人
if (i % k == 0) { j = i / k; }
else { j = i / k + 1; }
StudentDemo item = new StudentDemo(i, "张三" + i, j);
allStudents.Add(item);
}
}
/// <summary>
/// 设置所有的班级
/// </summary>
private void SettingAllClasses()
{
allClasses = new List<ClassDemo>();
for (int i = 1; i <= 3; i++)
{
ClassDemo item = new ClassDemo(i, "班级" + i);
allClasses.Add(item);
}
}
}
}