Share via


使用資料來源控制項繫結至資料

更新:2007 年 11 月

資料來源控制項能夠大幅擴展資料繫結控制項 (例如 GridViewFormViewDetailsView 控制項) 的功能。將資料來源控制項和資料繫結控制項搭配使用,可以讓您在幾乎不需要撰寫程式碼的情況下,擷取、修改、分頁、排序和篩選不同資料來源的資料。

使用 DataSourceID 屬性進行繫結

您可以將資料繫結控制項繫結至資料來源控制項,例如 LinqDataSourceObjectDataSourceSqlDataSource 控制項,便能使用資料繫結控制項中的資料。資料來源控制項會連接至資料來源 (例如資料庫、實體類別或中介層物件),再擷取或更新資料,接下來,資料繫結控制項就能夠使用這項資料。若要執行繫結,請將資料繫結控制項的 DataSourceID 屬性設定為指向資料來源控制項。當資料繫結控制項繫結至資料來源控制項時,幾乎不需要撰寫程式碼就可以執行資料作業。資料繫結控制項可以自動使用資料來源控制項所提供的資料服務。

注意事項:

在舊版的 ASP.NET 中,資料繫結控制項是使用 DataSource 屬性繫結至資料。要這麼做,您需要撰寫程式碼才能處理像顯示、分頁、排序、編輯和刪除資料等作業。雖然您可以使用 DataSource 屬性 (以及使用現有的程式碼) 將控制項繫結至資料,但是也可以改用 DataSourceID 屬性來執行資料繫結。一般而言,使用 DataSourceID 屬性所提供的自動功能,比使用 DataSource 屬性更多。

下列範例示範繫結至 LinqDataSource 控制項的 FormView 控制項,以顯示資料庫中的資料。若要讓範例運作,您必須建立代表資料庫和資料表的類別。您可以使用物件關聯式設計工具來建立這些類別。如需詳細資訊,請參閱物件關聯式設計工具 (O/R 設計工具)

<%@ 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 >
    <title>FormView Example</title>
</head>
<body>
    <form id="form1" >
      <h3>FormView Example</h3>
        <table cellspacing="10"> 
          <tr>               
            <td valign="top">

              <asp:FormView ID="ProductsFormView"
                DataSourceID="LinqDataSource1"
                AllowPaging="true"
                >

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

                <ItemTemplate>
                  <table>
                    <tr>
                      <td align="right"><b>Product ID:</b></td>
                      <td><asp:Label id="ProductIDLabel"  Text='<%# Eval("ProductID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Product Name:</b></td>
                      <td><asp:Label id="ProductNameLabel"  Text='<%# Eval("ProductName") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Category ID:</b></td>
                      <td><asp:Label id="CategoryIDLabel"  Text='<%# Eval("CategoryID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Quantity Per Unit:</b></td>
                      <td><asp:Label id="QuantityPerUnitLabel"  Text='<%# Eval("QuantityPerUnit") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Unit Price:</b></td>
                      <td><asp:Label id="UnitPriceLabel"  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"  
            >
        </asp:LinqDataSource>

      </form>
  </body>
</html>
<%@ Page language="C#" %>
<!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 >
    <title>FormView Example</title>
</head>
<body>
    <form id="form1" >
      <h3>FormView Example</h3>
        <table cellspacing="10"> 
          <tr>               
            <td valign="top">

              <asp:FormView ID="ProductsFormView"
                DataSourceID="LinqDataSource1"
                AllowPaging="true"
                >

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

                <ItemTemplate>
                  <table>
                    <tr>
                      <td align="right"><b>Product ID:</b></td>
                      <td><asp:Label id="ProductIDLabel"  Text='<%# Eval("ProductID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Product Name:</b></td>
                      <td><asp:Label id="ProductNameLabel"  Text='<%# Eval("ProductName") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Category ID:</b></td>
                      <td><asp:Label id="CategoryIDLabel"  Text='<%# Eval("CategoryID") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Quantity Per Unit:</b></td>
                      <td><asp:Label id="QuantityPerUnitLabel"  Text='<%# Eval("QuantityPerUnit") %>' /></td>
                    </tr>
                    <tr>
                      <td align="right"><b>Unit Price:</b></td>
                      <td><asp:Label id="UnitPriceLabel"  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"  
            >
        </asp:LinqDataSource>

      </form>
  </body>
</html>

如需資料來源控制項的詳細資訊,請參閱資料來源 Web 伺服器控制項

選取資料

資料來源控制項擷取資料的方式是由控制項本身決定。ObjectDataSource 控制項會呼叫 SelectMethod 屬性中指定的方法讀取資料。下列範例示範了使用 EmployeeLogic 類別之 GetAllEmployees 方法傳回資料的 ObjectDataSource 控制項。

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ 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>
    <title>ObjectDataSource - Visual Basic Example</title>
  </head>
  <body>
    <form id="Form1" method="post" >

        <asp:gridview
          id="GridView1"
          
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.VB.EmployeeLogic" />

    </form>
  </body>
</html>
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" >

        <asp:gridview
          id="GridView1"
          
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.CS.EmployeeLogic" />

    </form>
  </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %>
<%@ Page language="VJ#" %>
<!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>
    <title>ObjectDataSource - VJ# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" >

        <asp:gridview
          id="GridView1"
          
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.JSL.EmployeeLogic" />

    </form>
  </body>
</html>

如需詳細資訊,請參閱 ObjectDataSource Web 伺服器控制項概觀

SqlDataSourceAccessDataSource 控制項會執行 SelectCommand 屬性中所指定的 SQL 查詢來選取資料。下列範例示範了從 Northwind 範例資料庫的 Employees 資料表傳回資料的 SqlDataSource 控制項:

<%@ 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 >
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" >
      <asp:SqlDataSource
          id="SqlDataSource1"
          
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>
<%@ Page language="C#" %>

<!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 >
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" >
      <asp:SqlDataSource
          id="SqlDataSource1"
          
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>
<!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 >
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" >
      <asp:SqlDataSource
          id="SqlDataSource1"
          
          DataSourceMode="DataReader"
          ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

如需詳細資訊,請參閱使用 SqlDataSource 控制項選取資料

當您使用 LinqDataSource 控制項時,如果您沒有設定它的 Select 屬性,則控制項會傳回資料類別中所有屬性的資料。您可以設定 Select 屬性來指定屬性的子集或計算新的值。下列範例示範的 LinqDataSource 控制項,會從內含其他屬性的資料來源傳回三個屬性。

<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Select="new(Name, Category, Price)"
    ID="LinqDataSource1" 
    >
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    >
</asp:GridView>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Select="new(Name, Category, Price)"
    ID="LinqDataSource1" 
    >
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    >
</asp:GridView>

XmlDataSource 不允許您從來源 XML 資料選取特定項目。但是,您可以使用 XPath 屬性指定篩選條件。如需詳細資訊,請參閱使用 XmlDataSource 控制項篩選資料

修改資料

可以設定 LinqDataSourceObjectDataSourceSqlDataSource 控制項,如此使用者便能修改資料。如需詳細資訊,請參閱使用資料來源控制項修改資料

請參閱

其他資源

資料來源 Web 伺服器控制項