asp+|控制項 DataGrid3
DataGrid3通過添加可視格式化和內容格式化構建於DataGrid2之上。
摘自 DataGrid3.aspx:
〈%@ Page language="C#" src="DataGrid.cs" inherits="Samples.Data
GridPage"%〉
...
〈asp:DataGrid runat=server id="titlesGrid"
AutoGenerateColumns="false"
Width="80%"
BackColor="White"
BorderWidth="1px" BorderStyle="Solid" CellPadding="2" Cell
Spacing="0"
BorderColor="Tan"
Font-Name="宋體" Font-Size="8pt"〉
〈property name="Columns"〉
〈asp:BoundColumn headerText="Title" DataField="title"/〉
〈asp:BoundColumn headerText="Author" DataField="au_name"/〉
〈asp:BoundColumn headerText="Date Published" DataField=
"pubdate"
DataFormatString="{0:MMM yyyy}"/〉
〈asp:BoundColumn headerText="Price" DataField="price"
DataFormatString="{0:c}"〉
〈property name="ItemStyle"〉
〈asp:TableItemStyle HorizontalAlign="Right"/〉
〈/property〉
〈/asp:BoundColumn〉
〈/property〉
〈property name="headerStyle"〉
〈asp:TableItemStyle BackColor="DarkRed" ForeColor="White"
Font-Bold="true"/〉
〈/property〉
〈property name="ItemStyle"〉
〈asp:TableItemStyle ForeColor="DarkSlateBlue"/〉
〈/property〉
〈property name="AlternatingItemStyle"〉
〈asp:TableItemStyle BackColor="Beige"/〉
〈/property〉
〈/asp:DataGrid〉
此.aspx檔案顯示了與前面相同的DataGrid控制項聲明,並設定了各種
樣式屬性。這將導致視覺上更具吸引力的表示。仍就不需要對代碼進行任
何更改,使用與以前樣本相同的有代碼支援的檔案。
因為它是從 WebControl 得到的,所以 DataGrid 控制項繼承了諸如
Width、BackColor、BorderStyle 和 Font.Name 之類的樣式屬性。此外,
DataGrid提供諸如CellPadding這樣的屬性,這些屬性是特定於表的。這
些屬性允許從總體上定製控制項。
聲明還顯示了設定的若干項目樣式,如headerStyle和Alternating
ItemStyle。這些樣式控制著它們相應項目的外觀。請注意此樣本中出現
的樣式合并。備選項目與一般項目的前景色彩相同,因為它們的樣式是
AlternatingItemStyle和ItemStyle的組合。最後,此樣本還通過靠右對齊
價格列中的文本說明了為特定列設定樣式。
DataGrid還允許您格式化其儲存格中的常值內容。這是通過設定Bound
Column的DataFormatString屬性值完成的。該列使用其格式說明格式化使
用 String.Format的儲存格內容。此屬性可隨格式化類型(如日期或貨幣)
一起預置或附加任意內容。此外,由于格式化考慮了當前頁的CultureInfo
和請求,所以它也支援全域化。如果未指定格式,則使用該值的ToString
方法。
DataGrid4
DataGrid4 說明如何通過處理 SelectedIndexChanged 事件來利用
DataGrid 中的選擇。
截自 DataGrid4.aspx:
〈%@ Page language="C#" src="DataGrid4.cs" inherits="Samples.
DataGrid4Page"%〉
...
〈asp:DataGrid runat=server id="titlesGrid"
AutoGenerateColumns="false"
Width="80%"
BackColor="White"
BorderWidth="1px" BorderStyle="Solid" CellPadding="2"
CellSpacing="0"
BorderColor="Tan"
Font-Name="宋體" Font-Size="8pt"
DataKeyField="title_id"
OnSelectedIndexChanged="OnSelectedIndexChangedTitlesGrid"〉
〈property name="Columns"〉
〈asp:ButtonColumn Text="Select" Command="Select"/〉
〈asp:BoundColumn headerText="Title" DataField="title"/〉
〈asp:BoundColumn headerText="Author" DataField="au_name"/〉
〈asp:BoundColumn headerText="Date Published" DataField=
"pubdate"
DataFormatString="{0:MMM yyyy}"/〉
〈asp:BoundColumn headerText="Price" DataField="price"
DataFormatString="{0:c}"〉
〈property name="ItemStyle"〉
〈asp:TableItemStyle HorizontalAlign="Right"/〉
〈/property〉
〈/asp:BoundColumn〉
〈/property〉
〈property name="headerStyle"〉
〈asp:TableItemStyle BackColor="DarkRed" ForeColor="White"
Font-Bold="true"/〉
〈/property〉
〈property name="ItemStyle"〉
〈asp:TableItemStyle ForeColor="DarkSlateBlue"/〉
〈/property〉
〈property name="AlternatingItemStyle"〉
〈asp:TableItemStyle BackColor="Beige"/〉
〈/property〉
〈proper