欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

使用ExtJS GridPanel從Web Service 獲取、綁定和顯示數(shù)據(jù)_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:用Xaml做網(wǎng)頁框架
下面就開始編寫XAML,首先來定義一下頁面的屬性: 以下為引用的內(nèi)容: <Page xmlns="http://schemas.microsoft

本文將向大家介紹一種使用 ExtJS 的 GridPanel 組件從 ASP.NET Web Service 獲取 XML 數(shù)據(jù)并進(jìn)行綁定和顯示的方法。

GridPanel 組件在進(jìn)行數(shù)據(jù)綁定時(shí)可以接收多種數(shù)據(jù)格式。其中以 JSON 和 XML 最為常見。如果要給 GridPanel 綁定 JSON 格式的數(shù)據(jù)(這也是網(wǎng)上“通用”和“熱門”的解決方案),那么我們需要修改 Web Service 的 Web.config 設(shè)置,將輸出數(shù)據(jù)的格式改為 JSON(默認(rèn)為 XML。設(shè)置方法可參見:http://www.cnblogs.com/regedit/archive/2008/03/04/1089948.html 一文)。

個(gè)人認(rèn)為此方法過于繁瑣,為了保證對(duì)現(xiàn)有的 Web Service 不做較大的調(diào)整,我決定還是繼續(xù)讓其返回 XML 格式的數(shù)據(jù)。因此在對(duì) GridPanel 進(jìn)行數(shù)據(jù)綁定時(shí)就要選用 XML 方式綁定了。也就是說,我們?cè)诮壎〝?shù)據(jù)時(shí)要使用 Ext.data.XmlReader 而不是 Ext.data.JsonReader。具體的實(shí)現(xiàn)方法如下:

1. Web Service 部分:

以下為引用的內(nèi)容:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ArticleService : System.Web.Services.WebService {

// SQL 連接字符串
private readonly string _strConn = ConfigurationManager.ConnectionStrings["Junchieh"].ConnectionString;

// 從 start 開始 limit 行,按 sort 字段排序,排序方式為 dir(將傳入 "asc" 或 "desc")
[WebMethod]
public DataSet GetArticles(int start, int limit, string sort, string dir)
{
DataSet ds = new DataSet("Article");

// 從數(shù)據(jù)庫獲取數(shù)據(jù),放入 record 表
string strSql = string.Format(
"select top {0} * from Article where Id not in (select top {1} Id from Article order by {2} {3}) order by {2} {3}",
limit, start, sort, dir);
SqlDataAdapter da = new SqlDataAdapter(strSql, _strConn);
DataTable dtRecord = new DataTable("record");
lock (da)
{
da.Fill(dtRecord);
}
ds.Tables.Add(dtRecord);

// 計(jì)算總行數(shù),放入 results 表
DataTable dtResult = new DataTable("results");
dtResult.Columns.Add("totalRecords");
DataRow dr = dtResult.NewRow();
using (SqlConnection conn = new SqlConnection(_strConn))
using (SqlCommand cmd = new SqlCommand("select count(*) from Article", conn))
{
try
{
conn.Open();
dr["totalRecords"] = (int)cmd.ExecuteScalar();
}
catch
{
// do nothing
}
}
dtResult.Rows.Add(dr);
ds.Tables.Add(dtResult);

return ds;
}
}

GetAritcles 將返回一個(gè) DataSet。其內(nèi)包括兩個(gè) DataTable,第一個(gè)存放了(跟據(jù) start 和 limit 參數(shù)指定的)當(dāng)前頁的數(shù)據(jù),另一個(gè)存放了數(shù)據(jù)庫中所有數(shù)據(jù)的行數(shù),供客戶端的 GridPanel 組件使用。客戶端部分(節(jié)選):

以下為引用的內(nèi)容:

<script type="text/javascript">
Ext.onReady(function(){
// 列
var cm = new Ext.grid.ColumnModel([
{header:'ID', dataIndex:'Id', sortable:true, width:10},
{header:'標(biāo)題', dataIndex:'Title', sortable:true},
{header:'日期', dataIndex:'Date', sortable:true, width:50, renderer:renderDate} // 在 renderDate 函數(shù)中格式化
]);

// 數(shù)據(jù)源
var store = new Ext.data.Store({
url: 'Services/ArticleService.asmx/GetArticles', // Web Service 地址
reader: new Ext.data.XmlReader(
{
totalRecords: 'totalRecords', // 數(shù)據(jù)總行數(shù)。對(duì)應(yīng)于 GetArticles 返回的 DataSet 中的 results 表的 totalRecores 列
record: 'record', // 數(shù)據(jù)。對(duì)應(yīng)于 GetArticles 返回的 DataSet 中的 record 表
id: 'Id' // 主鍵。對(duì)應(yīng)于 GetArticles 返回的 DataSet 中的 record 表的 Id 列
},
[
{name: 'Id'},
{name: 'Title'},
{name: 'Date'}
]
),
remoteSort: true // 服務(wù)端排序
});
store.setDefaultSort('Date', 'desc'); // 默認(rèn)按 Date 列降序排列

// 分頁欄
var bbar = new Ext.PagingToolbar(
{
pageSize: 4,
store: store,
displayInfo: true,
displayMsg: '當(dāng)前顯示 {0} - {1} 條,共 {2} 條',
emptyMsg: "無數(shù)據(jù)"
}
);

// GridPanel 組件
var grid = new Ext.grid.GridPanel({
frame: true,
enableHdMenu: false,
width :600,
height:300,
title:'文章列表',
loadMask: {msg:'正在加載數(shù)據(jù),請(qǐng)稍侯……'},
el: 'grid',
store: store,
cm: cm,
bbar: bbar,
viewConfig: {
forceFit:true
}
});
grid.render();
store.load({params:{start:0,limit:4}}); // 初始時(shí)顯示第 1 頁,每頁顯示 4 條數(shù)據(jù)
});

// 格式化日期
// 將 XML 數(shù)據(jù)中的原始日期數(shù)據(jù)(如:2008-04-07T14:39:41.02 08:00)格式化成可讀的日期(如:2008-04-07 14:39:41)
function renderDate(value)
{
var reDate = /\d{4}\-\d{2}\-\d{2}/gi;
var reTime = /\d{2}:\d{2}:\d{2}/gi;
return value.match(reDate) " " value.match(reTime);
}
</script>

<!-- GridPanel 組件的顯示位置 -->
<div id="grid" style="height:300px; margin:auto;"></div>

從 Web Service 中由 GetArticles 方法返回的是 XML 數(shù)據(jù)。在將數(shù)據(jù)綁定到 Ext.data.Store 組件時(shí)應(yīng)使用 Ext.data.XmlReader 而不是網(wǎng)上經(jīng)常看到的 Ext.data.JsonReader。綁定時(shí)我們需要“告訴” XmlReader 在 XML 數(shù)據(jù)中哪些節(jié)點(diǎn)代表數(shù)據(jù)條目(本例為“record”),哪個(gè)節(jié)點(diǎn)代表數(shù)據(jù)總數(shù)(本例為“totalRecords”),以及數(shù)據(jù)的主鍵節(jié)點(diǎn) (本例為“Id”)。您可以通過下圖來理解 XmlReader 的數(shù)據(jù)綁定過程:

另外,從上圖中的 XML 數(shù)據(jù)可以看出,日期(Date)的格式比較“丑陋”,如果不加修飾的話將會(huì)原樣顯示于客戶的 GridPanel 組件中。因此在客戶端顯示數(shù)據(jù)之前,需要對(duì)日期數(shù)據(jù)進(jìn)行一下加工。在創(chuàng)建 Ext.grid.ColumnModel 時(shí)為 Date 列指定 renderer(注意加粗部分):

{header:'日期',dataIndex:'Date',sortable:true,width:50, renderer: renderDate}

代碼中的“renderDate”是一個(gè) JavaScript 函數(shù),定義如下:

以下為引用的內(nèi)容:
function renderDate(value)
{
var reDate = /\d{4}\-\d{2}\-\d{2}/gi;
var reTime = /\d{2}:\d{2}:\d{2}/gi;
return value.match(reDate) " " value.match(reTime);
}

此函數(shù)中的 value 參數(shù)即為原始的日期數(shù)據(jù),由 Ext.grid.ColumnModel 傳入。在函數(shù)中使用正則表達(dá)式分別提取日期數(shù)據(jù)中的“日期”和“時(shí)間”部分,拼接后返回。

整個(gè)程序執(zhí)行后的運(yùn)行界面如下圖所示:

分享:校內(nèi)網(wǎng)API的.net版本XiaoNei.Net 1.0(非官方)
校內(nèi)API開放也有一段時(shí)間了,也沒有太關(guān)注,正好新版本的SNS開發(fā)到了API這一塊,正好借鑒一下XiaoNei,F(xiàn)B,MySpace的API。 且聞校內(nèi)API比較有前途,最近的API編程大賽也比較火,而且人氣也較

來源:模板無憂//所屬分類:.Net教程/更新時(shí)間:2008-08-22
相關(guān).Net教程