技术开发 频道

Web页面数据绑定技术


【IT168技术文档】

  .NET 2.0和Visual Studio 2005为开发数据驱动的Web应用程序提供丰富、快捷的工具和方式。核心的功能就是数据绑定技术,通过数据绑定技术无须编写代码仅仅通过声明方式就可以实现如下的功能:

  — 读取和显示数据

  — 排序、分页和缓存数据

  — 更新、添加和删除数据

  — 利用运行时参数过滤数据

  — 利用参数方便地实现明细表显示

  数据绑定技术由两个部分组成:数据源控件(Data Source Controls)和数据绑定控件(Data-bound Controls)。

  数据源控件包括如下具体控件:

  — ObjectDataSource

  — SqlDataSource

  — AccessDataSource

  — XmlDataSource

  — SiteMapDataSource

  数据绑定控件包括如下具体控件:

  — 列表类控件,通过列表显示一系列数据,有BulletedList、CheckBoxList、DropDownList、ListBox和RadioButtonList控件。

  — AdRotator,显示广告。

  — DataList,把数据显示为一个表格。

  — DetailsView,把一条数据按照表格方式显示,并能编辑。

  — FormView,把一条数据按照自定义的布局显示,并能编辑。

  — GridView,支持编辑等功能的表格控件,并能很好地扩展。

  — Menu,菜单。

  — Repeater,按照自定义的布局显示一个数据的列表。

  — TreeView,树形控件。 

  通过数据绑定技术的这两项重要功能,我们能方便地开发绑定到数据库、XML数据和自定义对象集合的页面。

  绑定到数据库

  要绑定到数据库,如SQL Server,则只需使用SqlDataSource和相应的数据绑定控件,就可以轻易实现。如下面的例子就是通过一个GridView来显示、更新和删除NorthWind示例数据库中的Customers表的数据。
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>GridView Edit Example</h3> <!-- The GridView control automatically sets the columns --> <!-- specified in the datakeynames property as read-only. --> <!-- No input controls are rendered for these columns in --> <!-- edit mode. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" autogenerateeditbutton="true" datakeynames="CustomerID" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers SET CompanyName=@CompanyName, Address =@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
0
相关文章