c# web 用户控件的使用

来源:互联网 发布:牛仔外套品牌推荐知乎 编辑:程序博客网 时间:2024/05/23 00:01

1)新建web应用程序

2)添加类ClassDemo.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebApplication2{    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; }        }    }}

2)添加类StudentDemo.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebApplication2{    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; }        }    }}

3)添加用户控件ClassInfoControl.ascx

1.前台

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ClassInfoControl.ascx.cs" Inherits="WebApplication2.ClassInfoControl" %><asp:DataList ID="dlstStudent" runat="server"><ItemTemplate>    <table>        <tr><td><%#Eval("StudentId") %></td></tr>        <tr><td><%#Eval("StudentName") %></td></tr>    </table></ItemTemplate></asp:DataList><table>    <tr><td>---------------------------</td></tr></table>

2.后台

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication2{    public partial class ClassInfoControl : System.Web.UI.UserControl    {               protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                BindStudent();            }        }        /// <summary>        /// 班级学生(获取参数)        /// </summary>        private IEnumerable<StudentDemo> studentList;        public IEnumerable<StudentDemo> StudentList        {            get { return studentList; }            set { studentList = value; }        }        /// <summary>        /// 绑定班级学生信息        /// </summary>        private void BindStudent()        {            dlstStudent.DataSource = StudentList;            dlstStudent.DataBind();        }    }}

4)添加web窗体Default.aspx

1.前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._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 runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:Panel ID="Panel1" runat="server">        </asp:Panel>    </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 WebApplication2{    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()        {            foreach (ClassDemo item in allClasses)            {                IEnumerable<StudentDemo> list = GetStudentListByClassID(item.ClassID);                ClassInfoControl control = (ClassInfoControl)Page.LoadControl("ClassInfoControl.ascx");                control.ID = item.ClassID.ToString();                control.StudentList = list;                Panel1.Controls.Add(control);            }        }        /// <summary>        /// 根据班级编号获取学生        /// </summary>        /// <param name="classID"></param>        /// <returns></returns>        private IEnumerable<StudentDemo> GetStudentListByClassID(int classID)        {            IEnumerable<StudentDemo> list = null;            list = allStudents.Where(u => u.ClassID == classID);            return list;        }        /// <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);            }        }    }}


 

原创粉丝点击