web|資料 Working With the Data View Web Part
Microsoft® Office FrontPage® 2003
Author: Ben
MSN: benjamine65@hotmail.com
如何使用DataView調用XML Web Services
如何顯示父子從表
l 設計目標:
Data View綁定Web Service返回的資料集, 顯示父表, 同時以父表目前記錄關聯字估為條件, 嵌套顯示子表
l 資料結構(以下例子以Sql Server 2000的Northwind樣本資料庫作例子):
l 範例Web Service:
如有: http://localhost/AspNetSample/Service1.asmx Web Services, 其中關鍵代碼如:
[WebMethod]
public DataSet GetDataSource(string TableName)
{
DataSet ds = new DataSet("DataSetTables");
DataTable dt = new DataTable();
DataTable dtCus = new DataTable();
//sql
dt = SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, "select top 100 * from Orders").Tables[0];
dt.TableName = TableName;
dtCus = SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, "select top 100 * from Customers").Tables[0];
dtCus.TableName = "Customers";
ds.Tables.Add(dt.Copy());
ds.Tables.Add(dtCus.Copy());
return ds;
}
l 利用FrontPage 2003添加Data Source Catalog:
1. 開啟Task Pane. 下拉式功能表View -> Task Pane或Shortcut key Ctrl+F1 , 在Task Pane選擇Data Source Catalog, 展開XML Web Services並點擊Add to Catalog…
2. 在彈出的Data Source Properties視窗, 填General頁內容, 給當前資料來源起個名字, 例如: GetDataSource; 填Source頁內容, Service Description Location為http://localhost/AspNetSample/Service1.asmx?WSDL, OK後就Connect Now! 如果Web Services設定正確, 則在Connection Info裡會顯示相關的Service Name, Operation 等, 我們現在在Operation選GetDataSource, 設定一下GetDataSource的介面參數; 最後要設定的是Login頁的Login方法. 完成後就OK, XML Web Services下就會出現GetDataSource的表徵圖
3. 將GetDataSource拖到頁面的一個Web Part Zone內
4. 自訂Data View.
4.1. 插入一列, 並將游標置於新增列的儲存格內. 再轉換到Data View的Data View Details面板, 並選中Customers節點, 再Insert Subview
4.2. 設定關聯關係. 此時Customers的記錄會在顯示Orders記錄的Data View的那個新增的列內全部顯示出來, 還未會根據CustomerID顯示關聯的Customer記錄
所以現在要通過修改Data View 的XSL來實現關聯過濾.
分析:
點選Data View GetDataSource, 切換到Code 視圖, 找到關鍵的 XSL 語句, 如:
由此可以看出子表Customers是定義成一個xsl:template name=”dvt_2” 的, 我可以將CustomerID作為xsl:param傳遞到xsl:template裡作為過濾條件
1) 添加xsl:param
修改<xsl:call-template name="dvt_2"/> 為如下:
<xsl:call-template name="dvt_2">
<xsl: with-param name="CustomerID" select="CustomerID"/>
</xsl:call-template>
尋找dvt_2 template定義:
<xsl:template name="dvt_2">
<xsl:variable name="StyleName">Table</xsl:variable>
添加xsl:param:
<xsl:template name="dvt_2">
<xsl:param name="CustomerID"/>
<xsl:variable name="StyleName">Table</xsl:variable>
2) 應用xsl:param並實現過濾
將<xsl:template name="dvt_2">
<xsl:param name="CustomerID"/>
<xsl:variable name="StyleName">Table</xsl:variable>
<xsl:variable name="Rows" select="../Customers"/>
修改成:
<xsl:template name="dvt_2">
<xsl:param name="CustomerID"/>
<xsl:variable name="StyleName">Table</xsl:variable>
<xsl:variable name="Rows" select="../Customers[normalize-space(CustomerID) = $CustomerID]"/>