DataGrid Web控件深度历险(1)

** DataGrid Web ** ** 控件深度历险 ** ** (1) **

这篇文章是一系列关于使用 DataGrid Web 控件文章的第一部分。 ASP.Net DataGrid Web 控件可将数据库信息显示在 HTML 表格中,并且功能强大。在最简单的情形下 DataGrid 显示 HTML 表格框架,但是它可被增强以显示丰富的用户界面,可根据数据库的列进行排序,甚至允许对数据库结果进行分页!所有这些有趣的主题将在今后一系列文章中涉及。

从数据库中获取表格信息并将其显示在一个 HTML 表格中是传统 ASP 编程中最普通的任务之一。在传统 ASP 编程中需要通过多行交织的 HTML 和代码实现上述功能。下面的原形代码显示了这些代码通常的形式。

Create Database Connection  

Populate a recordset based on some SQL query  

Output the HTML table header (
1<table ...="">)  
2    
3    Loop through the recordset  
4    
5      Emit the HTML for a table row  
6    
7      ...  
8    
9    Emit the HTML table footer (</table>

)

如果你是一个 ASP 开发人员,你也许多次编写了上述代码。 ASP.Net 的优点之一就是它包含很多 Web 控件。这些产生 HTML 的 Web 控件提供了一个可编程的接口,它允许开发人员将代码和内容分离,并在代码中将产生 HTML 的实体作为对象使用。也就是说,如果我们需要通过 ASP.Net 显示一些 HTML 内容 , 将编写如下的代码:

1<script language="vb" runat="server">   
2sub Page_Load(sender as Object, e as EventArgs)   
3lblMessage.Text = "Hello, World!"   
4end sub   
5</script>
1<asp:label id="lblMessage" runat="server"></asp:label>

这里带有 runat=”server” 属性 ( 类似于 HTML 标记 ) 的 lblMessage Web 控件被放置在 HTML 中。然后,在 Page_Load 事件处理程序中(该事件处理程序在每次页面装载时被调用) lblMessage 的 Text 属性被设置为 ”Hello World” 。此处对于 Web 控件的使用,实现了代码和内容的分离。在传统的 ASP 中,需要将 ``` ="Hello, World!"

1
2` `
3
4` ** DataGrid  ** ` ` ** 基础  ** `
5
6要在  ASP.Net Web  页面中加入  DataGrid,  只需执行如下代码:

<asp:datagrid id="ID_of_DataGrid" runat="server"></asp:datagrid>

1这里的  id  值将作为在服务器端代码中使用  DataGrid  的名称,我们通过将上述语法放置在  HTML  中来使用  DataGrid  。但是为了让  DataGrid  显示任何有用的信息,我们需要将  DataGrid  绑定到一些信息的集合。这些信息的集合可以是任何支持  IEnumerable  接口的对象。它包括  Arrays,  集合类  (ArrayList ,Hashtable  等  )  ,  Datasets  和其它很多对象。由于希望集中精力显示数据库信息,因此在本文中我们仅关注将  DataGrid  绑定至  Datareader  。  Datareader  类似于传统  ADO/ASP  中顺序的  (forward-only)  记录集。  (  如需了解在  ADO.Net  中读取数据库结果至  Datareaders  中,请阅读  Efficiently Iterating Through Results from a Database Query using ADO.NET  ) 
2
3那么如何将数据绑定至  DataGrid?  其实出奇的简单。第一件事是提取数据库数据至  datareader.  对于本例,我使用  ASPFAQs.com  数据库,并且提取最受欢迎的  10  个问题。一旦将数据提取至  datareader,  将  datareader  绑定至  DataGrid  只需两行代码。第一行将  DataGrid  的  Datasource  属性设置为  Datareader  ;第二行调用  DataGrid  的  DataBind  方法,代码如下所示: 

@Import Namespace="System.Data"

@Import Namespace="System.Data.SqlClient"

<asp:datagrid id="dgPopularFAQs" runat="server"></asp:datagrid>

  1运行结果如下: 
  2    
  3    
  4    Simple DataGrid Demo
  5    
  6    
  7    This demo shows how to bind the results of a query to an unformatted DataGrid. 
  8
  9FAQID 
 10
 11| 
 12
 13Description 
 14
 15| 
 16
 17ViewCount 
 18
 19| 
 20
 21SubmittedByName 
 22
 23| 
 24
 25Submitted 
 26
 27ByEmail 
 28
 29| 
 30
 31Date 
 32
 33Entered 
 34
 35| 
 36
 37CatName   
 38  
 39---|---|---|---|---|---|---  
 40  
 41144 
 42
 43| 
 44
 45Where can I host my ASP Web site for _free_ (similar to GeoCities or Tripod or any of the many other free Web site sites)? 
 46
 47| 
 48
 49161056 
 50
 51| 
 52
 53Scott Mitchell 
 54
 55| 
 56
 57[email protected] 
 58
 59| 
 60
 613/20/2001 2:53:45 AM 
 62
 63| 
 64
 65Getting Started   
 66  
 67181 
 68
 69| 
 70
 71How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. 
 72
 73| 
 74
 75123888 
 76
 77| 
 78
 79Scott Mitchell 
 80
 81| 
 82
 83[email protected] 
 84
 85| 
 86
 871/19/2002 3:12:07 PM 
 88
 89| 
 90
 91ASP.NET   
 92  
 93 94
 95| 
 96
 97| 
 98
 99| 
100
101| 
102
103| 
104
105|   
106  
107首先注意用于编写数据绑定的代码数量不多。我们创建一个连接,指定一个  SQL  命令  (  这里使用一个存储过程,  sp_Popularity)  ,打开数据库连接,设定  DataGrid  的  DataSource  属性为  Datareader  ,最后调用  DataGrid  的  DataBind  方法。这种做法完全将代码从内容分离,没有像在传统  ASP  中混合  HTML  表格和  DataReader  输出的语法。 
108
109花些时间看一下运行结果。你会发现  DataGrid  使用  HTML  表格显示数据库内容,尽管并不美观。虽然我们完成了显示数据这一主要工作,但用户界面方面还有很多工作。幸运的是美化  DataGrid  的结果出奇的简单。遗憾的是需要等到下一篇文章中作介绍。 
110
111** 总结  **
112
113这是一系列关于  DataGrid  使用文章的一部分,我们研究了  DataGrid  最基本的功能  :  熟悉  ASP.Net Web  页面和显示绑定数据库结果。遗憾的是  DataGrid  的输出并不美观。但是我们不久会看到美化  DataGrid  的结果很简单。另外我们还将会在接下来的文章中看到更多用户界面的高级选项,如数据库结果的分页显示,  DataGrid  结果的排序和其它功能。
Published At
Categories with Web编程
Tagged with
comments powered by Disqus