c# web Datalist嵌套Datalist绑定数据

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);
            }
        }
    }
}


 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值