WebForm 擷取實體類的資料,填充頁面(用session傳值)

來源:互聯網
上載者:User

標籤:

<1>

首先建立一個實體類 User

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

<2>

調用SqlHelper將這個查詢出來的資料轉換成一個 list對象(將查詢出來的資料對應到實體類上)

</pre><pre name="code" class="csharp">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;                    }    }}

<3>

WebForm1.aspx.cs頁面 (注意:WebForm1.aspx頁面是繼承WebForm1.aspx.cs類的)

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>

WebForm1.aspx頁面

<%@ 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>


WebForm 擷取實體類的資料,填充頁面(用session傳值)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.