ASP.NET ObjectDataSource 分页排序 筛选
来源:互联网 发布:淘宝直通车关键词出价 编辑:程序博客网 时间:2024/06/08 04:41
分页
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetLimingchMember" TypeName="LimingchStudioUISorting"> </asp:ObjectDataSource>
CREATE PROCEDURE dbo.GetLimingchStudioPageData( @StartRowIndex INT, @MaximumRows INT)ASWITH 已编号的章立民研究室 AS( SELECT 员工号码, ROW_NUMBER() OVER (ORDER BY 员工号码) AS 序号 FROM 章立民研究室)SELECT 已编号的章立民研究室.序号, 章立民研究室.员工号码, 章立民研究室.姓名, 章立民研究室.性别, 章立民研究室.地址, 章立民研究室.部门FROM 已编号的章立民研究室 JOIN 章立民研究室 ON 已编号的章立民研究室.员工号码 = 章立民研究室.员工号码WHERE 序号 BETWEEN (@StartRowIndex + 1) AND (@startRowIndex + @maximumRows + 1);
public class LimingchStudioDataSourcePaging{ private string _connectionString; public LimingchStudioDataSourcePaging() { Initialize(); } public void Initialize() { // 初始化数据源。我们使用 Web.config 中名称为 chtNorthwind 的连接字符串。 if (ConfigurationManager.ConnectionStrings["chtNorthwind"] == null || ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString.Trim() == "") { throw new Exception("名称为 'chtNorthwind' 的连接字符串务必内含于 " + " <connectionStrings> 配置设置区段中。"); } // 将连接字符串的内容储存于变量 _connectionString 中。 _connectionString = ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString; } public DataTable GetLimingchMember(int startRowIndex, int maximumRows) { string commandText = "SELECT 员工号码,姓名,性别,地址,部门 FROM 章立民研究室"; // 建立一个数据配接器对象。 SqlDataAdapter da = new SqlDataAdapter(commandText, _connectionString); // 建立一个 DataSet 对象。 DataSet ds = new DataSet(); using (da) { // 从 startRowIndex 参数所指定的数据行开始,提取 maximumRows 参数所指定的笔数, // 然后将它们填入 DataSet 对象中的「章立民研究室」数据表。 da.Fill(ds, startRowIndex, maximumRows, "章立民研究室"); } // 传回 DataTable 物件。 if (ds.Tables["章立民研究室"] != null) return ds.Tables["章立民研究室"]; return null; } public int GetLimingchStudioCount() { HttpContext context = HttpContext.Current; if (context.Cache["LimingchStudioCount"] == null) { context.Cache["LimingchStudioCount"] = GetLimingchStudioCountFromSqlDB(); } return (int)context.Cache["LimingchStudioCount"]; } private int GetLimingchStudioCountFromSqlDB() { int nRows = 0; // 建立一个连接对象。 SqlConnection con = new SqlConnection(_connectionString); // 建立一个数据命令对象。 SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "SELECT Count(*) FROM 章立民研究室"; // 运行命令。 using (con) { con.Open(); nRows = (int)cmd.ExecuteScalar(); } return nRows; }}
分页排序
<asp:ObjectDataSource ID="IdName_ObjectDataSource" runat="server" SelectMethod="GetAllLimingchMember" TypeName="LimingchStudioParameterDsPagingSorting" EnablePaging="True" SelectCountMethod="GetLimingchStudioCount" SortParameterName="SortColumnExpression"> </asp:ObjectDataSource>
public class LimingchStudioDsPagingSorting{ private string _connectionString; public LimingchStudioDsPagingSorting() { Initialize(); } public void Initialize() { // 初始化数据源。我们使用 Web.config 中名称为 chtNorthwind 的连接字符串。 if (ConfigurationManager.ConnectionStrings["chtNorthwind"] == null || ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString.Trim() == "") { throw new Exception("名称为 'chtNorthwind' 的连接字符串务必内含于 " + " <connectionStrings> 配置设置区段中。"); } // 将连接字符串的内容储存于变量 _connectionString 中。 _connectionString = ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString; } // 请注意此方法是传回一个 SqlDataReader 对象,并且实作数据源分页机制与数据源排序机制。 public SqlDataReader GetLimingchMember(string SortColumnExpression, int startRowIndex, int maximumRows) { // 建立一个连接对象。 SqlConnection con = new SqlConnection(_connectionString); // 建立一个命令对象。 SqlCommand cmd = new SqlCommand(); cmd.Connection = con; // 设置运行 SQL Server 数据库中名称为 GetLimingchStudioPagedSortedData 的存储过程。 cmd.CommandText = "GetLimingchStudioPagedSortedData"; cmd.CommandType = CommandType.StoredProcedure; // 定义存储过程的输入参数 @SortColumnExpression 并将其值设置成 SortColumnExpression 参数。 cmd.Parameters.AddWithValue("@SortColumnExpression", SortColumnExpression); // 定义存储过程的输入参数 @StartRowIndex 并将其值设置成 startRowIndex 参数。 cmd.Parameters.AddWithValue("@StartRowIndex", startRowIndex); // 定义存储过程的输入参数 @MaximumRows 并将其值设置成 maximumRows 参数。 cmd.Parameters.AddWithValue("@MaximumRows", maximumRows); // 开启连接。 con.Open(); // 传回一个 SqlDataReader 物件。 return cmd.ExecuteReader(CommandBehavior.CloseConnection); } public int GetLimingchStudioCount() { int nRows = 0; // 建立一个连接对象。 SqlConnection con = new SqlConnection(_connectionString); // 建立一个数据命令对象。 SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "SELECT Count(*) FROM 章立民研究室"; // 运行命令。 using (con) { con.Open(); nRows = (int)cmd.ExecuteScalar(); } return nRows; }}
USE 北风贸易;GOIF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetLimingchStudioPagedSortedData]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[GetLimingchStudioPagedSortedData]GOCREATE PROCEDURE GetLimingchStudioPagedSortedData( @SortColumnExpression NVarChar(100), @StartRowIndex INT, @MaximumRows INT)AS-- 建立一个暂存数据表来储存所提取的数据。CREATE TABLE #MyTempPagedSortedTable( 序号 INT IDENTITY (1, 1) NOT NULL, 列号 INT)-- 将数据行新建至暂存数据表。INSERT INTO #MyTempPagedSortedTable (列号)SELECT 员工号码 FROM 章立民研究室ORDER BYCASE WHEN @SortColumnExpression='员工号码' THEN 员工号码 END ASC,CASE WHEN @SortColumnExpression='员工号码 DESC' THEN 员工号码 END DESC,CASE WHEN @SortColumnExpression='姓名' THEN 姓名 END ASC,CASE WHEN @SortColumnExpression='姓名 DESC' THEN 姓名 END DESC,CASE WHEN @SortColumnExpression='性别' THEN 性别 END ASC,CASE WHEN @SortColumnExpression='性别 DESC' THEN 性别 END DESC,CASE WHEN @SortColumnExpression='地址' THEN 地址 END ASC,CASE WHEN @SortColumnExpression='地址 DESC' THEN 地址 END DESC,CASE WHEN @SortColumnExpression='部门' THEN 部门 END ASC,CASE WHEN @SortColumnExpression='部门 DESC' THEN 部门 END DESC-- 取得特定分页的数据行SELECT 员工号码, 姓名, 性别, 地址, 部门FROM 章立民研究室 INNER JOIN #MyTempPagedSortedTable WITH (nolock) ON 章立民研究室.员工号码 = #MyTempPagedSortedTable.列号WHERE #MyTempPagedSortedTable.序号 > @StartRowIndex AND #MyTempPagedSortedTable.序号 < (@StartRowIndex + @MaximumRows + 1)ORDER BY #MyTempPagedSortedTable.序号
筛选
<asp:DropDownList ID="Department_DropDownList" runat="server" AutoPostBack="True" DataSourceID="Department_ObjectDataSource" DataTextField="部门" DataValueField="部门" Height="23px" Width="153px"> </asp:DropDownList> <asp:ObjectDataSource ID="Department_ObjectDataSource" runat="server" SelectMethod="GetLimingchDepartment" TypeName="LimingchStudioFilter"></asp:ObjectDataSource> <hr /> <asp:GridView ID="LimingchStudio_GridView" runat="server" DataSourceID="LimingchStudio_ObjectDataSource"> </asp:GridView> <asp:ObjectDataSource ID="LimingchStudio_ObjectDataSource" runat="server" EnableCaching="True" SelectMethod="GetLimingchMember" TypeName="LimingchStudioFilter" FilterExpression="部门='{0}'"> <FilterParameters> <asp:ControlParameter ControlID="Department_DropDownList" Name="部门" PropertyName="SelectedValue" /> </FilterParameters> </asp:ObjectDataSource>
public class LimingchStudioFilter{ private string _connectionString; public LimingchStudioFilter() { Initialize(); } public void Initialize() { // 初始化数据源。我们使用 Web.config 中名称为 chtNorthwind 的连接字符串。 if (ConfigurationManager.ConnectionStrings["chtNorthwind"] == null || ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString.Trim() == "") { throw new Exception("名称为 'chtNorthwind' 的连接字符串务必内含于 " + " <connectionStrings> 配置设置区段中。"); } // 将连接字符串的内容储存于变量 _connectionString 中。 _connectionString = ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString; } public SqlDataReader GetLimingchDepartment() { // 建立一个连接对象。 SqlConnection con = new SqlConnection(_connectionString); // 建立一个命令对象。 SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "SELECT DISTINCT 部门 FROM 章立民研究室"; // 开启连接。 con.Open(); // 传回一个 SqlDataReader 物件。 return cmd.ExecuteReader(CommandBehavior.CloseConnection); } public DataTable GetLimingchMember() { string commandText = "SELECT 员工号码,姓名,性别,地址,部门 FROM 章立民研究室"; // 建立一个数据配接器对象。 SqlDataAdapter da = new SqlDataAdapter(commandText, _connectionString); // 建立一个 DataSet 对象。 DataSet ds = new DataSet(); using (da) { // 将数据填入 DataSet 对象中的「章立民研究室」数据表。 da.Fill(ds, "章立民研究室"); } // 传回 DataTable 物件。 if (ds.Tables["章立民研究室"] != null) return ds.Tables["章立民研究室"]; return null; }}
- ASP.NET ObjectDataSource 分页排序 筛选
- Asp.net学习笔记----使用GridView+ObjectDataSource进行自定义分页排序
- Asp.net 利用Repeater和ObjectDataSource 实现自定义分页控件
- ObjectDataSource-ASP.NET 2.0与ObjectDataSource控件
- 排序、 筛选和分页与 ASP.NET MVC 应用程序中的实体框架
- Asp.Net Ajax+WCF Service+ADO.NET Entity Framework实现数据列表,数据筛选,分页,排序,删除
- 用DataList控件和ObjectDataSource在ASP.NET 2.0中实现高效能数据分页
- 用DataList控件和ObjectDataSource在ASP.NET 2.0中实现高效能数据分页
- .net 对已有的DataTable进行筛选、排序、分页
- 使用 ASP.NET 2.0 ObjectDataSource 控件
- 使用 ASP.NET 2.0 ObjectDataSource 控件
- 使用 ASP.NET 2.0 ObjectDataSource 控件
- 使用 ASP.NET 2.0 ObjectDataSource 控件
- 使用 ASP.NET 2.0 ObjectDataSource 控件
- 学习总结:关于ASP.NET 中的 ObjectDataSource
- ASP.NET 2.0 ObjectDataSource控件用法实例
- 使用 ASP.NET 2.0 ObjectDataSource 控件
- ASP.NET - ObjectDataSource Web 服务器控件概述
- [flex]点语法汇总
- [Visual C++系列]窗口控件 - 4.1 Button
- u-boot编译方法
- 噪声测量仪 源代码
- [Visual C++系列]窗口控件 - 4.2 Static Box
- ASP.NET ObjectDataSource 分页排序 筛选
- nand 与 nor flash 的区别
- poj 2546 两圆相交求面积
- ABAP:SmartForms --盘点表设计
- 广东一伙劫匪在抢劫银行时说了一句至理名言
- Android开发环境搭建全程演示(jdk+eclipse+android sdk)
- [Visual C++系列]窗口控件 - 4.3 Edit Box
- 用javascript实现jquery的document.ready功能的实现代码
- PostGrep中的schem