Custom Paging using Object Data Source
<table width="100%"> <tr> <td> <GridView ID="grdUpdates" runat="server" Width="100%" AutoGenerateColumns="false" DataKeyNames="id" AllowPaging="true" DataSourceID="srcFiling" PageSize="10" PagerStyle-HorizontalAlign="Center" BackColor="#DCE1EC" RowStyle-Font-Names="verdana" RowStyle-Font-Size="11px" BorderStyle="Solid" BorderWidth="2px"> </GridView>
// </td> </tr> <tr> <td> // ObjectDataSource ID="srcFiling" TypeName="PagingClass" SelectMethod="GetFiling" SelectCountMethod="GetFilingCount" EnablePaging="True" runat="server" // </td> </tr> </table>
// 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.SqlClient; using System.Web.Configuration;
public class PagingClass { public static string _conString = WebConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; public static SqlDataReader GetFiling(int startRowIndex, int maximumRows) { // Initialize connection SqlConnection con = new SqlConnection(_conString); // Initialize command SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "GetFiles"; cmd.CommandType = CommandType.StoredProcedure; // Add ADO.NET parameters cmd.Parameters.AddWithValue("@StartRowIndex", startRowIndex); cmd.Parameters.AddWithValue("@MaximumRows", maximumRows); // Execute command con.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } public static int GetFilingCount() { HttpContext context = HttpContext.Current; if (context.Cache["FilingCount"] == null) context.Cache["FilingCount"] = GetFilingCountFromDB(); return (int)context.Cache["FilingCount"]; } private static int GetFilingCountFromDB() { int result = 0; // Initialize connection SqlConnection con = new SqlConnection(_conString); // Initialize command SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "SELECT Count(*) FROM view_ownership"; // Execute command using (con) { con.Open(); result = (int)cmd.ExecuteScalar(); } return result; }
}
//
// ALTER PROCEDURE dbo.GetFiles ( @StartRowIndex INT, @MaximumRows INT ) AS -- Create a temp table to store the select results CREATE TABLE #PageIndex ( IndexId INT IDENTITY (1, 1) NOT NULL, RecordId INT ) -- INSERT into the temp table INSERT INTO #PageIndex (RecordId) SELECT Id FROM view_ownership -- Get a page of movies SELECT Id, shareholder, secfileno, formtypeId, fileddate, filedbyId, cusip, Issuername, formtype,cik,flag FROM view_ownership INNER JOIN #PageIndex WITH (nolock) ON view_ownership.Id = #PageIndex.RecordId WHERE #PageIndex.IndexID > @startRowIndex AND #PageIndex.IndexID < (@startRowIndex + @maximumRows + 1) ORDER BY view_ownership.fileddate desc --#PageIndex.IndexID
//
|
No responses found. Be the first to respond and make money from revenue sharing program.
|