(本文撰寫於 ASP.NET 1.x 時期,但觀念、做法亦適用 ASP.NET 2.0)

在 ASP.NET 1.0 中,最火紅的資料顯示控件非 DataGrid 莫屬 (ASP.NET 2.0 的 GridView 亦同),其可顯示儲存在 Web server 記憶體中,DataSet/DataTable 裡的「表格式資料」。但在 ASP.NET 頁面中要處理「表格式資料」,事實上還有另一種較不起眼的 Table 控件 (不同於 DataTable)。該「顯示型」Table 控件雖然內建的功能有限,但自由度反而較高,可由程序員自行撰寫程式碼去設計表格的外觀,包括:可「跨欄、跨列」即時顯示從數據庫撈出的資料;以及自訂依每個「儲存格 (TableCell)」裡的數值不同,動態顯示不同的顏色。所以 Table 控件等於是一個「空心的」顯示型控件,很多特性和方法它都不提供,必須由程序員手工打造,但也因此少掉許多包袱,並可能創作出比其它控件更強大的功能。

不過透過 Table 控件顯示的「表格式資料」,無法在 Post-back 後保存下來,表格內容必須在每次 Post-back 後再重新建構。根據 MSDN Library 的說法,若預期會進行大量的修改,建議改用 DataList 或 DataGrid 控件來代替 Table 控件。

替 ASP.NET 的 Table 控件換裝
圖 1 Table 控件結構圖

上圖 1 為 Table 控制項的物件結構,每一個「儲存格」等於一個 TableCell 物件,同一列的所有 TableCell 構成一個 TableRow 物件,而所有 TableRow 物件構成一整個 Table 控件。

下圖 2 是版工以兩種不同寫法,所繪製出的兩個 Table 控件。程式碼 (VB.NET/ASP.NET 1.x) 可由本帖最下方的超連結下載。
替 ASP.NET 的 Table 控件換裝
圖 2 依「儲存格」數值變化,動態顯示不同顏色


範例一:第一個 Table 控制項 (合併資料列)

替 ASP.NET 的 Table 控件換裝<Html>
替 ASP.NET 的 Table 控件換裝
<Body>
替 ASP.NET 的 Table 控件換裝
<H2>特殊表格的製作</H2>
替 ASP.NET 的 Table 控件換裝
<ASP:Table Runat="Server" GridLines="Both" CellPadding="4" id="Table1" HorizontalAlign="Center"> 
替 ASP.NET 的 Table 控件換裝
<ASP:TableRow Runat="Server">
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
<ASP:TableCell Runat="Server" Text="姓名" BackColor="LightGreen"/>
替 ASP.NET 的 Table 控件換裝
<ASP:TableCell Runat="Server" Text="Stephen"/>
替 ASP.NET 的 Table 控件換裝
<ASP:TableCell Runat="Server" RowSpan="2">
替 ASP.NET 的 Table 控件換裝  
<ASP:Image Runat="Server" ImageUrl="image/money.jpg" Width="40" Height="40"/>
替 ASP.NET 的 Table 控件換裝
</ASP:TableCell>
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
</ASP:TableRow>
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
<ASP:TableRow>
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
<ASP:TableCell Runat="Server" Text="電子郵件" BackColor="LightGreen"/>
替 ASP.NET 的 Table 控件換裝
<ASP:TableCell Runat="Server">
替 ASP.NET 的 Table 控件換裝  
<ASP:HyperLink Runat="Server" Text="j2se@pchome.com.tw" NavigateUrl="mailto:j2se@pchome.com.tw"/>
替 ASP.NET 的 Table 控件換裝
</ASP:Tablecell>
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
</ASP:TableRow>
替 ASP.NET 的 Table 控件換裝
</ASP:Table>
替 ASP.NET 的 Table 控件換裝
<p>
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
<ASP:Table Runat="Server" GridLines="Both" CellPadding="4" id="Table2" HorizontalAlign="Center" />
替 ASP.NET 的 Table 控件換裝
替 ASP.NET 的 Table 控件換裝
</Body>
替 ASP.NET 的 Table 控件換裝
</Html>

上方的範例一當中,使用了 RowSpan (合併資料列),其為 TableCell 控件中內建的屬性,除此之外該控件還提供 ColumnSpan 屬性。

下方範例二的股票行情表,會依數據庫中撈出的數值,即時性地在 TableCell 中顯示不同顏色。您在使用上可依專案需求,將某些特定顯示功能寫成副程式或函數。


範例二:第二個 Table 控制項 (依「儲存格」數值變化,動態顯示不同顏色),執行畫面如上圖 2

在範例二中,ASP.NET 頁面用 DataReader 讀取資料,再把取得的資料填入 Table 控件中。您還可在範例二中,再加上「跨欄、跨列」的功能。至於還能達成哪些應用,則端看程序員的巧思了。

-------------------------------------------------
本帖的範例源碼下載點:
https://files.cnblogs.com/WizardWu/060106.zip

-------------------------------------------------
(本文在版工的舊 Blog 中,發表日期為 2006/01/06)
 

 

相关文章:

  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-05-30
  • 2022-02-11
  • 2022-01-22
猜你喜欢
  • 2021-12-03
  • 2021-11-20
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案