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

來源:互聯網
上載者:User

WebForm 擷取實體類的資料,填充頁面(用session傳值),webformsession
用AJAX非同步方式來擷取資料,並在頁面動態展示資料

<1>

建立一個UserService類。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;namespace ModelService{    public class UserService    {        public static string SelectDataToJson()        {            //將從資料庫查詢出來的資料儲存到 一個DataTable中            DataTable dt= SqlHelper.ExecuteDataTable("select * from T_User");            //將這個dt錶轉換成Json格式 樣本:{"Json":[{"Id":"1","UserName":"張三"}]}            string JsonStr = DataTableToJson.DtToJson("Json", dt);            //返回這個Json格式的字串            return JsonStr;        }    }}


<2>

建立一個Handler1.ashx  (讓AJAX請求從這裡獲得資料)

using System;using System.Collections.Generic;using System.Linq;using System.Web;using 實體類.ModelService;namespace 實體類{    /// <summary>    /// Handler1 的摘要說明    /// </summary>    public class Handler1 : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            //調用UserService類下的SelectDataToJson()方法,擷取到一個Json格式的字串(字串儲存了執行SQL語句查詢到的資料)            string str = UserService.SelectDataToJson();            //將資料寫到瀏覽器            context.Response.Write(str);                     }        public bool IsReusable        {            get            {                return false;            }        }    }}

<3>

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>    <script type="text/javascript" src="jquery-1.11.2.js"></script>    <script type="text/javascript">        <!--一個Post類型AJAX非同步請求-->        $.post("Handler1.ashx", function (strJson) { //strJson是請求成功後伺服器返回的資料            //strJson的值為{"Json":[{"Id":"1","UserName":"張三","Age":"25","Gender":"0"},{"Id":"2","UserName":"李四","Age":"26","Gender":"1"}]}            var oJson = $.parseJSON(strJson); //將這個資料轉換成Json格式            //oJson.Json是擷取key為Json這個對象的值;因為這個值為一個數組,那麼.length就擷取到了這個數組的長度(為2)            var oJsonLength = oJson.Json.length;            //遍曆這個數組的長度()            for (var i = 0; i < oJsonLength; i++) {                //當i=0的時候oJson.Json[i]就代表數組的第一個元素,這個元素又是一個Json對象。所以就可以通過.Id來獲得資料了                var tr = "<tr><td>" + oJson.Json[i].Id + "</td><td>" + oJson.Json[i].UserName + "</td><td>" + oJson.Json[i].Age + "</td><td>" + oJson.Json[i].Gender + "</td></tr>";                $("#t1").append(tr); //將這個tr字串添加到id為t1的這個Table中去            }        })    </script></head><body>    <form id="form1" runat="server">    <div>    <table id="t1" border="1px">       <tr><td>編號</td><td>姓名</td><td>年齡</td><td>性別</td></tr>            </table>    </div>       </form></body></html>


用Session傳值的形式,將資料傳遞到WebForm.aspx頁面來展示資料

<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對象(將查詢出來的資料對應到實體類上)

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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.