对使用数据源显示信息的web服务器控件,使用数据源控件绑定到数据
发布日期:2022-03-15 11:49:55 浏览次数:32 分类:技术文章

本文共 3986 字,大约阅读时间需要 13 分钟。

使用数据源控件绑定到数据

10/22/2014

本文内容

更新:2007 年 11 月

数据源控件大大扩展了诸如 GridView、FormView 和 DetailsView 控件等数据绑定控件的功能。通过将数据源控件与数据绑定控件一起使用,无需编写代码或只需编写少量代码即可对不同数据源中的数据进行检索、修改、分页、排序和筛选。

使用 DataSourceID 属性进行绑定

您可以将数据绑定控件绑定到数据源控件(例如 LinqDataSource、ObjectDataSource 或 SqlDataSource 控件),这样便可以在数据绑定控件中使用数据。数据源控件连接到数据库、实体类或中间层对象等数据源,然后检索或更新数据。之后,数据绑定控件即可使用此数据。要执行绑定,应将数据绑定控件的 DataSourceID 属性设置为指向数据源控件。当数据绑定控件绑定到数据源控件时,无需编写代码或者只需编写少量额外代码即可执行数据操作。数据绑定控件可以自动利用数据源控件提供的数据服务。

0bccfc35e74eaedf0b3cde0a215a40a0.gif说明:

在早期版本的 ASP.NET 中,通过使用 DataSource 属性将数据绑定控件绑定到数据。这要求您编写代码来处理显示、分页、排序、编辑和删除数据等操作。虽然可以使用 DataSource 属性(并使用现有代码)将控件绑定到数据,但也可以改用 DataSourceID 属性执行数据绑定。通常,使用 DataSourceID 属性可以比使用 DataSource 属性提供更多自动功能。

下面的示例演示绑定到 LinqDataSource 控件以显示数据库中的数据的 FormView 控件。要使示例工作,必须创建用于表示数据库和表的类。通过使用对象关系设计器可以创建这些类。有关更多信息,请参见对象关系设计器(O/R 设计器)。

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

FormView Example

FormView Example

DataSourceID="LinqDataSource1"

AllowPaging="true"

runat="server">

Product ID:
Product Name:
Category ID:
Quantity Per Unit:
Unit Price:

ContextTypeName="NorthwindDataContext"

TableName="Products"

ID="LinqDataSource1"

runat="server">

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

FormView Example

FormView Example

DataSourceID="LinqDataSource1"

AllowPaging="true"

runat="server">

Product ID:
Product Name:
Category ID:
Quantity Per Unit:
Unit Price:

ContextTypeName="NorthwindDataContext"

TableName="Products"

ID="LinqDataSource1"

runat="server">

有关数据源控件的更多信息,请参见数据源 Web 服务器控件。

选择数据

数据源控件检索数据的方式由控件本身决定。ObjectDataSource 控件通过调用 SelectMethod 属性中指定的方法来读取数据。下面的示例演示一个 ObjectDataSource 控件,该控件使用 EmployeeLogic 类的 GetAllEmployees 方法返回数据。

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

ObjectDataSource - Visual Basic Example

id="GridView1"

runat="server"

datasourceid="ObjectDataSource1" />

id="ObjectDataSource1"

runat="server"

selectmethod="GetAllEmployees"

typename="Samples.AspNet.VB.EmployeeLogic" />

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

ObjectDataSource - C# Example

id="GridView1"

runat="server"

datasourceid="ObjectDataSource1" />

id="ObjectDataSource1"

runat="server"

selectmethod="GetAllEmployees"

typename="Samples.AspNet.CS.EmployeeLogic" />

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

ObjectDataSource - VJ# Example

id="GridView1"

runat="server"

datasourceid="ObjectDataSource1" />

id="ObjectDataSource1"

runat="server"

selectmethod="GetAllEmployees"

typename="Samples.AspNet.JSL.EmployeeLogic" />

SqlDataSource 和 AccessDataSource 控件通过运行 SelectCommand 属性中指定的 SQL 查询来选择数据。下面的示例演示一个 SqlDataSource 控件,该控件从 Northwind 示例数据库的 Employees 表中返回数据。

ASP.NET Example

id="SqlDataSource1"

runat="server"

DataSourceMode="DataReader"

ConnectionString=""

SelectCommand="SELECT LastName FROM Employees">

id="ListBox1"

runat="server"

DataTextField="LastName"

DataSourceID="SqlDataSource1">

ASP.NET Example

id="SqlDataSource1"

runat="server"

DataSourceMode="DataReader"

ConnectionString=""

SelectCommand="SELECT LastName FROM Employees">

id="ListBox1"

runat="server"

DataTextField="LastName"

DataSourceID="SqlDataSource1">

ASP.NET Example

id="SqlDataSource1"

runat="server"

DataSourceMode="DataReader"

ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"

SelectCommand="SELECT LastName FROM Employees">

id="ListBox1"

runat="server"

DataTextField="LastName"

DataSourceID="SqlDataSource1">

使用 LinqDataSource 控件时,如果不设置其 Select 属性,该控件将返回数据类中所有属性的数据。通过设置 Select 属性,可以指定属性的子集或计算新值。下面的示例演示一个 LinqDataSource 控件,该控件返回包含其他属性的数据源中的三个属性。

ContextTypeName="ExampleDataContext"

TableName="Products"

Select="new(Name, Category, Price)"

ID="LinqDataSource1"

runat="server">

DataSourceID="LinqDataSource1"

ID="GridView1"

runat="server">

ContextTypeName="ExampleDataContext"

TableName="Products"

Select="new(Name, Category, Price)"

ID="LinqDataSource1"

runat="server">

DataSourceID="LinqDataSource1"

ID="GridView1"

runat="server">

XmlDataSource 不允许从源 XML 数据中选择特定元素。不过,您可以使用 XPath 属性指定筛选器。有关更多信息,请参见 使用 XmlDataSource 控件筛选数据。

修改数据

请参见

其他资源

转载地址:https://blog.csdn.net/weixin_34574003/article/details/119622324 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:微信免密支付服务器忙,微信免密支付 查询用户授权记录,调起商家预授权 都返回系统繁忙...
下一篇:ubuntu系统服务器维护,Ubuntu Server系列各项服务的安装和维护 Apache部分

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月19日 02时34分13秒