使用 Web 标准生成 ASP.NET 2.0 Web 站点 |
| 作者:Stephen Walther 来源:Stephen Walther 发布时间:2006-4-27 1:19:21 |
|
可以使用 summary 属性提供浏览器未呈现的表的说明。另一方面,浏览器呈现 标记的内容。您应当使用 标记来标识表的用途。 如果使用 ASP.NET 2.0 GridView 或 DetailsView 控件来显示 HTML 表中的数据库数据,则默认情况下,生成的 HTML 表是可访问的。例如,清单 7 包含一个 ASP.NET 页,它通过使用 GridView 控件来显示 Titles 数据库表的内容。 清单 7. DisplayTitles.aspx <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Display Titles</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView id="grdTitles" DataSourceId="srcTitles" Runat="server" /> <asp:SqlDataSource id="srcTitles" ConnectionString= "Server=localhost;Trusted_Connection=true;Database=Pubs" SelectCommand="Select * FROM Titles" Runat="server" /> </div> </form> </body> </html> 在清单 7 中,GridView 控件被绑定到一个表示 Titles 数据库表中记录的 SqlDataSource 控件。在浏览器中打开清单 7 中的 ASP.NET 页时,Titles 数据库表的内容显示在 HTML 表中(参见图 10)。 图 10. DisplayTitles.aspx 请注意,GridView 控件自动为每个列标题生成 <th> 标记。而且,如果在浏览器中选择 View Source,则可以看到为每个列标题自动生成 scope="col" 属性。 GridView 控件支持其他多个与可访问性相关的属性: • Caption 和 CaptionAlign — 使用这些属性可以向 GridView 控件生成的 HTML 表中添加标题。 • RowHeaderColumn — 使用该属性可以指示行标题(相对于列标题而言)。请将该属性设置为从数据源返回的列的名称(例如,title_id)。 • UseAccessibleHeader — 使用该属性可以指示是否应当用 或 <td> 标记呈现列标题。默认情况下,该属性具有值 true。 请注意,GridView 控件不具有 Summary 属性。但是,像大多数 ASP.NET 控件一样,GridView 控件支持 expando 属性。当您声明 GridView 控件时,您可以声明喜欢的任何属性,而该属性将被呈现到浏览器中。因此,如果您希望向 GridView 中添加摘要,则请按如下方式声明 summary 属性。 <asp:GridView id="grdTitles" DataSourceId="srcTitles" summary="Displays the contents of the Titles database table" Runat="server" /> GridView 控件的默认行为非常适合于以可访问的方式显示简单数据表。但是,如果您需要显示更为复杂的表(例如,一组嵌套表),则您必须完成额外的工作。 例如,您可能希望显示产品类别的列表,并且希望在每个类别下显示匹配产品的列表。换句话说,您希望创建单页“主要信息/详细信息”表单(参见图 11)。在这种情况下,需要为每个表单元格包含 headers 属性。 图 11. 嵌套 Repeater 控件 清单 8 中的页说明如何将一个 Repeater 控件嵌套到另一个 Repeater 控件中,以及如何生成符合可访问性准则要求的复杂表。 清单 8. NestedRepeaters.aspx <%@ Page Language="VB"%> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Private dtblProducts As New DataTable Sub Page_Load() Dim dad As New SqlDataAdapter("SELECT * FROM PRODUCTS", _ "Server=localhost;Trusted_Connection=true;Database=Northwind") dad.Fill(dtblProducts) End Sub Function GetProducts(ByVal CategoryID As Integer) As DataView Dim view As DataView = dtblProducts.DefaultView view.RowFilter = "CategoryID=" & CategoryID Return view End Function Function GetCategoryHeader(ByVal index As Integer) As String Return "hdrCategory" & index.ToString() End Function Function GetProductHeader(ByVal productID As Integer) As String Return "hdrProduct" & productID.ToString() End Function Function GetHeaders(ByVal item As RepeaterItem, _ ByVal columnHeader As String) As String Dim parent As RepeaterItem = _ item.NamingContainer.NamingContainer Dim categoryHeader As String = _ GetCategoryHeader(parent.ItemIndex) Dim productHeader As String = _ GetProductHeader(item.DataItem("ProductID")) Return String.Format("{0} {1} {2}", categoryHeader, _ productHeader, columnHeader) End Function </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <style type="text/css"> .categoryRow {background-color:yellow} </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater id="grdCategories" DataSourceId="srcCategories" Runat="server"> <HeaderTemplate> <table> <thead> <th id="hdrID">ID</th> <th id="hdrName">Name</th> <th id="hdrPrice">Price</th> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr class="categoryRow"> <th colspan="3" id='<%# GetCategoryHeader(Container.ItemIndex) %>'> <%# Eval("CategoryName") %> </th> </tr> <asp:Repeater id="grdProducts" DataSource='<%# GetProducts(Eval("CategoryID")) %>' Runat="server"> <ItemTemplate> <tr> <th id='<%# GetProductHeader(Eval("ProductID")) %>'> <%# Eval("ProductID") %> </th> <td headers='<%# GetHeaders(Container, "hdrName") %>'> <%#Eval("ProductName")%> </td> <td headers= '<%# GetHeaders(Container, "hdrPrice") %>'> <%#Eval("UnitPrice", "{0:c}")%> </td> </tr> </ItemTemplate> </asp:Repeater> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource id="srcCategories" ConnectionString= "Server=localhost;Trusted_Connection=true;Database=Northwind" SelectCommand="SELECT * FROM Categories" Runat="server" /> </div> </form> </body> </html> 在清单 8 中,外层的 Repeater 控件用来列出产品类别,而内层的 Repeater 控件用来列出匹配产品。下列两个 Helper 函数用来生成 Category Name 和 Product ID 标题的 id 值:GetProductHeader 和 GetCategoryHeader 函数。另外一个单独的名为 GetHeaders 的 Helper 函数用 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] 下一页 |
| [] [返回上一页] [打 印] |
|
文章评论 |
