Home > Article > Web Front-end > WebForm gets the data of the entity class and fills the page (use session to pass value)_html/css_WEB-ITnose
<1>
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace 实体类{ public class User { public int Id { get; set; } public string UserName { get; set; } public int Age { get; set; } public int Gender { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;namespace 实体类.ModelService{ public class UserService { public static List<User> SelectDataToEntity() { //通过查询数据库,将获取到的数据转换成一个list List<User> list = SqlHelper.SelectDataToList<User>("select * from T_User"); return list; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using 实体类.ModelService;namespace 实体类{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //将获取到的list(List<User> lsit)存放到 Session里。然后可以在WebForm1.aspx页面中来遍历这个list 从而取到实体类的数据 Session.Add("User", UserService.SelectDataToEntity()); } }}
<4>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="实体类.WebForm1" %><!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> <table border="1px"> <% var list = (List<实体类.User>)Session["User"];%> <1--获取到这个key为User的Session,强转为List<实体类.User>类型--> <tr><td>编号</td><td>姓名</td><td>年龄</td><td>性别</td></tr> <% foreach( var r in list){ %> <tr><td><%=r.Id %></td><td><%=r.UserName %></td><td><%=r.Age %></td><td><%=r.Gender %></td></tr> <% }%> </table> </div> </form></body></html>