ASP.NET - 使用数据源控件绑定到数据

 

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

 

一、使用 DataSourceID 属性进行绑定

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

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

      下面的示例演示绑定到 LinqDataSource 控件以显示数据库中的数据的 FormView 控件。要使示例工作,必须创建用于表示数据库和表的类。通过使用对象关系设计器可以创建这些类。

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

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

<html  >

  <head runat="server">

    <title>FormView Example</title>

</head>

<body>

    <form id="form1" runat="server">

      <h3>FormView Example</h3>

        <table cellspacing="10">

          <tr>              

            <td valign="top">

 

              <asp:FormView ID="ProductsFormView"

                DataSourceID="LinqDataSource1"

                AllowPaging="true"

                runat="server">

 

                <HeaderStyle forecolor="white" backcolor="Blue" />               

 

                <ItemTemplate>

                  <table>

                    <tr>

                      <td align="right"><b>Product ID:</b></td>

                      <td><asp:Label id="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /></td>

                    </tr>

                    <tr>

                      <td align="right"><b>Product Name:</b></td>

                      <td><asp:Label id="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></td>

                    </tr>

                    <tr>

                      <td align="right"><b>Category ID:</b></td>

                      <td><asp:Label id="CategoryIDLabel" runat="server" Text='<%# Eval("CategoryID") %>' /></td>

                    </tr>

                    <tr>

                      <td align="right"><b>Quantity Per Unit:</b></td>

                      <td><asp:Label id="QuantityPerUnitLabel" runat="server" Text='<%# Eval("QuantityPerUnit") %>' /></td>

                    </tr>

                    <tr>

                      <td align="right"><b>Unit Price:</b></td>

                      <td><asp:Label id="UnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' /></td>

                    </tr>

                  </table>                

                </ItemTemplate>

 

                <PagerTemplate>

                  <table>

                    <tr>

                      <td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td>

                      <td><asp:LinkButton ID="PrevButton"  CommandName="Page" CommandArgument="Prev"  Text="<"  RunAt="server"/></td>

                      <td><asp:LinkButton ID="NextButton"  CommandName="Page" CommandArgument="Next"  Text=">"  RunAt="server"/></td>

                      <td><asp:LinkButton ID="LastButton"  CommandName="Page" CommandArgument="Last"  Text=">>" RunAt="server"/></td>

                    </tr>

                  </table>

                </PagerTemplate>

 

              </asp:FormView>

 

            </td>

          </tr>

        </table>

 

        <asp:LinqDataSource

            ContextTypeName="NorthwindDataContext"

            TableName="Products"

            ID="LinqDataSource1" 

            runat="server">

        </asp:LinqDataSource>

 

      </form>

  </body>

</html>

 

二、选择数据

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

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>

<%@ Page language="c#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

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

<html  >

  <head>

    <title>ObjectDataSource - C# Example</title>

  </head>

  <body>

    <form id="Form1" method="post" runat="server">

 

        <asp:gridview

          id="GridView1"

          runat="server"

          datasourceid="ObjectDataSource1" />

 

        <asp:objectdatasource

          id="ObjectDataSource1"

          runat="server"

          selectmethod="GetAllEmployees"

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

 

    </form>

  </body>

</html>

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

<%@ Page language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html  >

  <head runat="server">

    <title>ASP.NET Example</title>

</head>

<body>

    <form id="form1" runat="server">

      <asp:SqlDataSource

          id="SqlDataSource1"

          runat="server"

          DataSourceMode="DataReader"

          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"

          SelectCommand="SELECT LastName FROM Employees">

      </asp:SqlDataSource>

 

      <asp:ListBox

          id="ListBox1"

          runat="server"

          DataTextField="LastName"

          DataSourceID="SqlDataSource1">

      </asp:ListBox>

 

    </form>

  </body>

</html>

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

<asp:LinqDataSource

    ContextTypeName="ExampleDataContext"

    TableName="Products"

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

    ID="LinqDataSource1"

    runat="server">

</asp:LinqDataSource>

<asp:GridView

    DataSourceID="LinqDataSource1"

    ID="GridView1"

    runat="server">

</asp:GridView>

      XmlDataSource 不允许从源 XML 数据中选择特定元素。不过,您可以使用 XPath 属性指定筛选器。

 

三、修改数据

可以配置 LinqDataSourceObjectDataSource SqlDataSource 控件,以便用户可以修改数据。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值