利用C#實(shí)現(xiàn)HTML模板的循環(huán)輸出
關(guān)于模板循環(huán)輸出
數(shù)據(jù)感應(yīng)也即數(shù)據(jù)捆綁,是一種動態(tài)的,Web控件與數(shù)據(jù)源之間的交互,模板循環(huán)輸出 ,是指使用 UI 前端設(shè)計的 HTML 模板片斷,并結(jié)合數(shù)據(jù)記錄進(jìn)行循環(huán)輸出的過程,比如,有如下如圖輸出 :
如圖人員列表是一個循環(huán)輸出的過程,我們按照圖示設(shè)計模板,并結(jié)合數(shù)據(jù)查詢結(jié)果即可,模板代碼如下:
<div id="pitemdetail" runat="server" class="query-box" onclick="waittip(this);window.location='answer.aspx?cid={wxmpCid}&pid={cid}'" style="border-radius:5px; cursor:pointer; padding:10px; background-color:White; margin-bottom:10px"> <div style="display:flex;flex-direction:row; flex-wrap:wrap;justify-content:space-between; "><label style="cursor:pointer; font-size:12pt; color:Gray; " > <b>{name}</b></label>{dtip}</div> <div style="width:100%; color:Silver; ">{dname}</div> <div></div> </div><!-- pitemdetail --> <asp:Literal ID="result" runat="server"></asp:Literal>
其中 id 為 pitemdetail 的 div 即為設(shè)計模板片斷,其設(shè)計要點(diǎn)如下:
(1)需要添加 runat="server" 服務(wù)器標(biāo)記
(2)輸出的數(shù)據(jù),用花括號和字段名表示,如代碼中的 "{wxmpCid}","{name}" 。字段名可以用DataSet.Tables.Colums 里的序號進(jìn)行表示,如 “{0}”,“{1}” ,用序號表示,性能會略有提升,但從代碼易讀性來講相對較差。
(3)不可在模板中放置其它服務(wù)器控件
id 為 result 的 Literal 控件(用于顯示最原始狀態(tài)靜態(tài)文本的輸出控件),隸屬于Microsoft.Web.UI.WebControls 集合,該控件用于存儲并顯示最后的輸出結(jié)果。
本文將介紹如何中通過 C# 實(shí)現(xiàn)操作 HTML 模板的循環(huán)輸出。
準(zhǔn)備數(shù)據(jù)源
我們在 MS SQL Server 創(chuàng)建 CCVC_PriceList(支付卡等級表),其結(jié)構(gòu)如下表:
序號 | 字段名 | 類型 | 說明 |
---|---|---|---|
1 | [PName] | [nvarchar](10) | 產(chǎn)品名稱 |
2 | [CCVC] | [int] | 初始積分 |
3 | [Price] | [money] | 產(chǎn)品價格 |
4 | [LogoUrl] | [nvarchar](500) | 產(chǎn)品Logo |
執(zhí)行如下 創(chuàng)建表的 SQL 語句:
CREATE TABLE [dbo].[CCVC_PriceList]( [PName] [nvarchar](50) NOT NULL, [CCVC] [int] NOT NULL, [Price] [money] NOT NULL, [LogoUrl] [nvarchar](500) NULL, ) GO
執(zhí)行如下SQL語句,創(chuàng)建一些數(shù)據(jù):
insert into CCVC_PriceList(PName,CCVC,Price,LogoUrl]) values('禮遇卡',24000,1900.00,'v1.jpg'); insert into CCVC_PriceList(PName,CCVC,Price,LogoUrl]) values('金卡',56000,6900.00,'v2.jpg'); insert into CCVC_PriceList(PName,CCVC,Price,LogoUrl]) values('白金卡',120000,15900.00,'v3.jpg'); insert into CCVC_PriceList(PName,CCVC,Price,LogoUrl]) values('鉆石卡',210000,18900.00,'v4.jpg'); insert into CCVC_PriceList(PName,CCVC,Price,LogoUrl]) values('至尊卡',1000000,78900.00,'v5.jpg');
通過查詢分析器,執(zhí)行查詢SQL語句,顯示如下圖:
最后我們將數(shù)據(jù)填充到 DataReader ,并生成對應(yīng)的二維數(shù)組。
范例運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
數(shù)據(jù)庫:Microsoft SQL Server 2016
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
RepeatHtml 方法
設(shè)計與實(shí)現(xiàn)
RepeatHtml 方法主要是通過 object[,] 二維對象數(shù)組數(shù)據(jù)源進(jìn)行提取并根據(jù)模板 HTML 循環(huán)輸出到指定的接收控件上,其參數(shù)設(shè)置見下表:
序號 | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | Html | string | 要輸出的 HtmlTable 對象 |
GetReaderData 方法可以訪問數(shù)據(jù)庫數(shù)據(jù)表進(jìn)行查詢結(jié)果的提取,并轉(zhuǎn)化為 object[,] 二維數(shù)組
RepeatHtml 方法實(shí)現(xiàn)代碼如下:
ArrayList paras=new ArrayList(); string refSql=""; bool HasTitle=false; System.Data.CommandType ct=System.Data.CommandType.Text; public string RepeatHtml(string Html) { string rv = ""; object[,] ReaderData = GetReaderData("SqlServer","您的連接串",refSql,paras,hastitle,ct); if (ReaderData == null) return ""; if (HasTitle == true) { for (int k = 0; k < ReaderData.GetLength(1); k++) //列 { string fieldname = ReaderData[0,k].ToString(); Html = Html.Replace("{" + fieldname + "}", "{" + k.ToString() + "}"); } } for (int i = (HasTitle == true ? 1 : 0); i < ReaderData.GetLength(0); i++) //行 { object[] repl = new object[ReaderData.GetLength(1)]; for (int j = 0; j < ReaderData.GetLength(1); j++) //列 { repl[j] = ReaderData[i, j].ToString() ; } rv+=string.Format(Html, repl); } return rv; }//RepeatHtml
如何獲取模板內(nèi)容
獲取服務(wù)器控件的內(nèi)容元素可劃分為兩個范圍,類似 JavaScript 里的 innerHTML(獲取 HTML 元素內(nèi)部的內(nèi)容,即元素的子節(jié)點(diǎn),不包括元素本身的標(biāo)簽)和 outerHTML(除了包含 innerHTML 的全部內(nèi)容外,還包含對象標(biāo)簽本身,用于獲取 HTML 元素及其包含的內(nèi)容的完整HTML表示,包括元素本身在內(nèi)),下面我們將逐一實(shí)現(xiàn)這兩種服務(wù)器方法:
getOuterHtml 方法
getOuterHtml 通過傳遞服務(wù)器控件參數(shù),獲取服務(wù)器控件完整的HTML元素內(nèi)容,代碼如下:
public string getOuterHtml(Control ctl){ System.Text.StringBuilder strb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(strb); System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw); ctl.RenderControl(htw); string str = strb.ToString(); return str; }
getInnerHtml 方法
getInnerHtml 基于 getOuterHtml 方法,獲取服務(wù)器控件內(nèi)部元素的所有內(nèi)容,傳遞參數(shù)用法相同,實(shí)現(xiàn)代碼如下:
public string getInnerHtml(Control ctl) { string str = ""; for (int j = 0; j < ctl.Controls.Count; j++) { str += getOuterHtml(ctl.Controls[j]); } return str; }
調(diào)用示例
客戶端模板設(shè)計代碼如下:
<div id="pitemdetail" runat="server" style=" width:270px; border-radius:10px; cursor:pointer; padding:10px; background-color:WhiteSmoke; margin-bottom:10px"> <div style="display:flex;flex-direction:row; flex-wrap:wrap;justify-content:space-between; "> <img src="{LogoUrl}" width="125" height="75" /><div><span style="font-size:14pt;">{PName}</span><div style="width:100%; color:Gray;font-size:9pt "><br>售價:{Price}元<br>贈積分:{CCVC}</div></div></div> </div><!-- pitemdetail --> <asp:Literal ID="result" runat="server"></asp:Literal>
服務(wù)端示例代碼如下:
string refSql="SELECT [PName],[CCVC],[Price], LogoUrl FROM [CCVC_PriceList] order by CCVC"; bool HasTitle=false; System.Data.CommandType ct=System.Data.CommandType.Text; pitemdetail.Visible = true; string itemmodule = getOuterHtml(pitemdetail); result.Text = RepeatHtml(itemmodule); pitemdetail.Visible = false;
提示:pitemdetail 模板塊,需要在調(diào)用前顯示,調(diào)用后隱藏。
調(diào)用成功顯示效果如下圖所示:
小結(jié)
RepeatHtml 方法一般配合 getOuterHtml 方法使用,如果運(yùn)行出現(xiàn)服務(wù)器字符串格式錯誤,請檢查花括號輸出的字段是否存在以及大小寫情況,盡量與SQL語句輸出保持一致。以上就是關(guān)于模板循環(huán)輸出的介紹,我們可以根據(jù)自己的實(shí)際需要進(jìn)行改造,本示例代碼僅供您參考。
以上就是利用C#實(shí)現(xiàn)HTML模板的循環(huán)輸出的詳細(xì)內(nèi)容,更多關(guān)于C#模板循環(huán)輸出的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)凍結(jié)Excel窗口以鎖定行列或解除凍結(jié)
在處理大型Excel工作簿時,有時候我們需要在工作表中凍結(jié)窗格,這樣可以在滾動查看數(shù)據(jù)的同時保持某些行或列固定不動,下面我們就來看看如何使用C#實(shí)現(xiàn)凍結(jié)Excel窗口吧2024-04-04C#實(shí)現(xiàn)窗體中動態(tài)按鈕的設(shè)計方法
在窗體界面中,通常以按鈕來代替菜單欄的功能,這種形式雖然給用戶一種直觀、界面風(fēng)格各異的感覺,但通常按鈕都是以靜止的形式顯示,所以本文給大家介紹了C#實(shí)現(xiàn)窗體中動態(tài)按鈕的設(shè)計方法,感興趣的朋友可以參考下2024-04-04C#如何遠(yuǎn)程讀取服務(wù)器上的文本內(nèi)容
這篇文章主要介紹了C#如何遠(yuǎn)程讀取服務(wù)器上的文本內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01C#將Sql數(shù)據(jù)保存到Excel文件中的方法
這篇文章主要介紹了C#將Sql數(shù)據(jù)保存到Excel文件中的方法,文中的ExportExcel可起到將sql數(shù)據(jù)導(dǎo)出為Excel的作用,需要的朋友可以參考下2014-08-08