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); } } }}
- c# web 用户控件的使用
- hoyi-web[java] 用户控件的使用
- C#的Winform中使用用户控件
- 使用C#的用户控件创建ActiveX
- 使用C#开发用户控件
- C#的WEB控件
- ASP.NET母版页和Web用户控件的使用
- C# 中的用户自定义控件和WPF的配合使用
- Asp.Net中Web用户控件与Web自定义控件的区别和使用建议
- C#用户控件的创建
- c#用户控件的生命周期
- 用户控件的使用
- 用户控件的使用
- 使用IE Web 控件和Web用户控件
- 开发和使用Web用户控件
- 开发和使用Web用户控件
- 开发和使用Web用户控件
- 开发和使用Web用户控件
- HTML头部,元信息
- 模板缓冲区的使用
- 家事国事我调侃
- 深入理解iPhone委托模式兼谈iPhone生命周期
- 怎样使UISearchBar背景透明
- c# web 用户控件的使用
- iphone上UISwitch控件用法
- JSON 解析 - java库org.json简介
- 使用倍增算法(Prefix Doubling)构造后缀数组
- 迷宫问题
- 关于多核的发展对网络游戏设计影响的一些思考
- MediaWiki 1.17.0 安装
- WIN32 数据类型 理解
- 跨平台网络库的设计