ASP.NET - 使用数据源控件筛选数据

 

      数据源控件提供多种数据服务,这样将高级功能添加到应用程序会变得更加容易。这包括根据指定的搜索条件筛选数据。筛选对于使用缓存数据尤其方便,因为您无需重新运行查询或调用读取数据的方法就可以提供搜索功能。

      若要筛选数据,必须按下列方式配置数据源控件:

·对于 SqlDataSource AccessDataSource 控件,必须将 DataSourceMode 属性设置为 DataSet

·对于 ObjectDataSource 控件,基础源对象必须返回 DataSet DataTable 对象。

使用 XmlDataSource 控件时,可以使用 XPath 查询筛选数据。

 

一、设置筛选器表达式

通过设置数据源控件的 FilterExpression 属性,指定要应用到 ObjectDataSourceSqlDataSource AccessDataSource 控件所返回的数据的筛选器。筛选器表达式的语法基于 DataColumn 类的 Expression 属性的语法。当调用数据源控件的 Select 方法时,将应用筛选器表达式。

 

二、提供筛选器参数

您可以为 ObjectDataSourceSqlDataSource AccessDataSource 控件提供参数化的筛选器表达式,这使您可以在运行时提供筛选值,而无需编写任何代码来显式设置 FilterExpression 属性。使用数据源控件的 FilterParameters 集合指定筛选器表达式参数。参数可以检索控件中的数据、QueryString 对象、会话状态、用户配置文件属性等等。

      在筛选器表达式中,创建与数据源控件的 FilterParameters 集合中的项对应的占位符。占位符是带编号的,0 表示集合中的第一个参数。通过将筛选器参数的编号放入“{”和“}”字符中间,可以指定筛选器表达式的占位符,如以下示例所示:

       Country = '{0}' AND LastName LIKE '{1}'

      安全说明: 因为 FilterParameters 集合中的值被替换为没有编码的 FilterExpression 字符串,所以在应用筛选器之前应当验证所有筛选器参数的值。在应用筛选器之前,可以使用数据源控件的 Filtering 事件访问并验证筛选器参数的值。

      下面的示例演示一个包含筛选器参数、名为 EmployeeDetailsSqlDataSource SqlDataSource 控件。FilterExpression 属性中使用的参数值在运行时从页上其他位置的控件的属性值进行填充。

<%@ 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>Northwind Employees</title>

</head>

<body>

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

 

      <h3>Northwind Employees</h3>

 

        <table cellspacing="10">           

          <tr>

            <td valign="top">

              <table border="0">

                <tr>

                  <td valign="top">Country</td>

                  <td><asp:DropDownList runat="server" id="CountryListBox" AppendDataBoundItems="True"

                                        DataSourceID="CountrySqlDataSource"

                                        DataTextField="Country" DataValueField="Country" >

                        <asp:ListItem Selected="True" Value="" >(Show All)</asp:ListItem>

                      </asp:DropDownList>

                  </td>

                </tr>

                <tr>

                  <td>Last Name</td>

                  <td><asp:TextBox runat="server" id="LastNameTextBox" Text="*" /></td>

                </tr>

                <tr>

                  <td></td>

                  <td><asp:Button runat="server" id="FilterButton" Text="Filter Results" /></td>

                </tr>

              </table>

            </td>

 

            <td valign="top">               

              <asp:GridView ID="EmployeesGridView"

                DataSourceID="EmployeeDetailsSqlDataSource"

                AutoGenerateColumns="false"

                AllowSorting="true"

                DataKeyNames="EmployeeID"    

                Gridlines="Both"

                RunAt="server">

 

                <HeaderStyle backcolor="Navy"

                  forecolor="White"/>

 

                <RowStyle backcolor="White"/>

 

                <AlternatingRowStyle backcolor="LightGray"/>

 

                <EditRowStyle backcolor="LightCyan"/>

 

                <Columns>                 

                  <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                   

                  <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>

                  <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                   

                  <asp:BoundField DataField="Country"    HeaderText="Country"/>                   

                </Columns>                

              </asp:GridView>

            </td>               

          </tr>           

        </table>

 

        <asp:SqlDataSource ID="CountrySqlDataSource"

          SelectCommand="SELECT DISTINCT Country FROM Employees"

          EnableCaching="True"

          CacheDuration="60"

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

          RunAt="server" />

 

        <asp:SqlDataSource ID="EmployeeDetailsSqlDataSource"

          SelectCommand="SELECT EmployeeID, LastName, FirstName, Country FROM Employees"

          EnableCaching="True"

          CacheDuration="60"

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

          FilterExpression="Country LIKE '{0}' AND LastName LIKE '{1}'"

          RunAt="server">

 

          <FilterParameters>

            <asp:ControlParameter ControlID="CountryListBox"   PropertyName="SelectedValue" />

            <asp:ControlParameter ControlID="LastNameTextBox" PropertyName="Text" />

          </FilterParameters>

        </asp:SqlDataSource>

      </form>

  </body>

</html>

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值