Gridview、ObjectDataSource 让生活更轻松






2.88/5 (11投票s)
本文将简要介绍如何在 Gridview 中使用 ObjectDataSource 实现排序、搜索和分页。
引言
在本文中,我将简要介绍如何在 Gridview 中使用 ObjectDataSource 实现排序、搜索和分页。网上有很多文章演示了相同的功能,但它们使用了 SqlDataSources,这在实际项目中是不可行的。ObjectDataSource 可以保持应用程序的三层架构。
先决条件
· SQL Server 2000(示例应用程序使用 Northwind 数据库)
· Visual Studio 2005
· EntLib 2.0(数据访问)
示例应用程序
本文使用的应用程序包含:
· Default.aspx
· BizLayer.cs
· DataAccess.cs
在此应用程序中,Gridview 将使用对象数据源来实现自动分页、排序等功能。Object data source 将使用 bizlayer 方法进行选择和搜索。
示例应用程序架构
入门
将 Gridview 和 Object data source 从工具箱的数据选项卡拖放到 default.aspx 上,完成后您将看到类似以下的内容:
现在,通过点击上图所示的“配置数据源”来配置数据源,向导将引导我们完成后续步骤。
填充 Gridview(Select 方法)
在此方法中,我们将选择 Northwind 数据库 Customers 表中的所有记录。
步骤 1:选择您的业务对象
步骤 2:从下拉列表中选择 BizLayer。
步骤 3:选择业务对象中的一个方法,该方法将返回与 select 操作相关的数据。在我们的应用程序中,我们将从下拉列表中选择 GetCustomers 方法。
步骤 4:为我们的 grid 选择数据源。点击 grid 的智能标签
步骤 5:从“选择数据源”下拉列表中选择 odsNorthWind(对象数据源)。
启用自动分页/排序
选择 grid 的数据源后,我们将看到一个菜单,其中勾选“启用分页和排序”选项以实现自动分页/排序。就这样,我们的 grid 将无需编写任何代码即可具备分页和排序功能,这不是很棒吗?
搜索
在需要过滤 grid 中数据的情况下,这是一个非常常见的情景。Object data source 提供了一个属性来实现此功能。我们只需要为 object data source 的 FilterExpression 属性提供搜索字符串即可,就这样 J。
FilterExpression 属性:根据 MSDN 的说法,获取或设置在调用 SelectMethod 属性指定的方法时应用的过滤表达式。
在示例应用程序中,我们根据公司名称进行了搜索,因此该属性的 filterexpression 可以这样编码:
protected void btnSearch_Click(object sender, EventArgs e)
{
if (txtCustName.Text != "")
{
odsNorthWind.FilterExpression = "CompanyName LIKE '%" + txtCustName.Text + "%'";
}
}
更新
同样,更新也是数据操作应用程序中一个非常常见的情景。在示例应用程序中,我们将更新选定客户的联系人姓名。
编辑 Gridview 数据
要启用 Gridview 行的编辑功能,首先为 Gridview 添加一个编辑列。为此,请先单击 Gridview 智能标签,然后单击“编辑列”链接,然后从“命令字段”中添加“编辑”、“更新”、“删除”命令,如下所示:
点击“确定”。
在示例应用程序中,grid 只有来自客户表的三个列和一个命令列。
Gridview 编辑的代码操作
1. 将 BoundField 的 ReadOnly 属性设置为 false,以便在 Gridview 中编辑所有字段。
2. 将 Gridview 的 DataKeyNames 属性设置为表的 ID。在示例应用程序中,DataKeyNames="CustomerID"
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Customer ID"
ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="Company Name"
ReadOnly="True" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
ReadOnly="false" SortExpression="ContactName" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<asp:GridView ID="gvCustomers" runat="server" DataKeyNames="CustomerID" AllowPaging="True" AllowSorting="True" CellPadding="4" DataSourceID="odsNorthWind" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" EmptyDataText="No Record Available">
向 Object Data Source 添加更新方法
最后一步是向数据源控件添加更新方法。为此,请单击 ObjectDataSource 的智能标签,然后单击“配置数据源”,再单击“下一步”,然后选择“更新”选项卡。在“选择方法”下拉列表中选择一个更新方法,如下所示。在示例应用程序中,我们选择了 UpdateCustomer 方法。
示例应用程序代码
BizLayer.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class BizLayer
{
public BizLayer()
{
//
// TODO: Add constructor logic here
//
}
public DataSet GetCustomers()
{
DataAccess dataAcess = null;
try
{
dataAcess = new DataAccess();
return dataAcess.GetCustomers();
}
finally
{
dataAcess = null;
}
}
public void UpdateCustomers(string CustomerID,string contactName)
{
DataAccess dataAcess = null;
try
{
dataAcess = new DataAccess();
dataAcess.UpdateCustomers(CustomerID, contactName);
}
finally
{
dataAcess = null;
}
}
}
DataAccess.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
public class DataAccess
{
public DataAccess()
{
//
// TODO: Add constructor logic here
//
}
public DataSet GetCustomers()
{
DataSet dsGetCustomer = null;
DbCommand dbCommand = null;
Database db = DatabaseFactory.CreateDatabase("Northwind");
dbCommand = db.GetSqlStringCommand("select * from Customers");
dsGetCustomer = db.ExecuteDataSet(dbCommand);
return dsGetCustomer;
}
public void UpdateCustomers(string CustomerID,string contactName)
{
DbCommand dbCommand = null;
Database db = DatabaseFactory.CreateDatabase("Northwind");
dbCommand = db.GetSqlStringCommand("update Customers SET ContactName = '"+contactName+"' where CustomerID = '" + CustomerID+"'");
db.ExecuteNonQuery(dbCommand);
}
}
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (txtCompanyName.Text != "")
{
odsNorthWind.FilterExpression = "CompanyName LIKE '%" + txtCompanyName.Text + "%'";
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title>GridView and ObjectDataSource making Life easy</title>
</head>
<body>
<form id="form1" runat="server">
<table border="0" cellpadding="0" width="100%">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Enter Company Name :"></asp:Label>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="gvCustomers" runat="server" DataKeyNames="CustomerID" AllowPaging="True" AllowSorting="True"
CellPadding="4" DataSourceID="odsNorthWind" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" EmptyDataText="No Record Available">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Customer ID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="Company Name" ReadOnly="True" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsNorthWind" runat="server" SelectMethod="GetCustomers"
TypeName="BizLayer" UpdateMethod="UpdateCustomers">
<UpdateParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="contactName" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>