حالا يه صفحه اي كه اين Pager رو تو خودش داره. يه DropDownList هم اضافه كردم كه كاربر بتونه تعداد ركوردهاي هر صفحه رو انتخاب كنه. اينجا از Event ي كه تعريف كرده بوديم براي نمايش شماره صفحه اي كه كاربر داره روش كليك مي كنه استفاده مي كنيم. يه تابع داريم كه اطلاعات رو از بانك اطلاعاتي بارگذاري مي كنه. مي تونيد به اين تابع شماره صفحه و تعداد ركورد و فيلتر و فيلدي براي مرتب سازي داده ها هم ارسال كنيد.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="FanaCustomControls" Namespace="FanaCustomControls" TagPrefix="fcc" %>
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>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage" runat="server">asp:Label><br />
<asp:DropDownList ID="ddlPageSize" runat="server">
<asp:ListItem>10asp:ListItem>
<asp:ListItem>20asp:ListItem>
<asp:ListItem>100asp:ListItem>
asp:DropDownList>
<fcc:DataPaging ID="DataPaging1" runat="server" />
div>
form>
body>
html>
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
DataPaging1.SelectedPageChanged += new EventHandler(SelectedPageChanged);
DataPaging1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
int RecordCount = 0;
//RecordCount=SELECT * FROM Table WHERE Condition
DataPaging1.RecordCount = RecordCount;
ViewState["RecordCount"] = RecordCount;
BindDataList(1, Convert.ToInt32(ddlPageSize.SelectedValue), "Date", "[Name] LIKE '%hh%'");
}
catch (Exception exp)
{
lblMessage.Text = exp.Message;
}
}
protected void SelectedPageChanged(object sender, EventArgs e)
{
Response.Write(string.Format("شما در حال مشاهده صفحه {0} هستید", DataPaging1.PageIndex.ToString()));
BindDataList(DataPaging1.PageIndex, Convert.ToInt32(ddlPageSize.SelectedValue), "Date", "[Id]>0");
}
private void BindDataList(int pageIndex, int pageSize, string sortField, string filterString)
{
try
{
string _ConnectionString = "";
SqlConnection connection = new SqlConnection(_ConnectionString);
SqlCommand command = new SqlCommand();
command.Connection = connection;
//You should bind your datalist to database here using a query like this
string query = "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY @SortField) AS RowNum, [Field1], [Field2] FROM [TableName] WHERE @FilterString) AS MainQuery WHERE RowNum>=@StartRecord AND RowNum<@EndRecord ";
command.CommandText = query;
command.Parameters.AddWithValue("@SortField", sortField);
command.Parameters.AddWithValue("@FilterString", filterString);
int startRecord = Convert.ToInt32(ViewState["RecordCount"]) / pageSize * pageIndex;
int endRecord = startRecord + pageSize;
command.Parameters.AddWithValue("@StartRecord", startRecord);
command.Parameters.AddWithValue("@EndRecord", sortField);
connection.Open();
SqlDataReader dataReader;
dataReader = command.ExecuteReader();
while (dataReader.Read())
{
//Enter Records In a ArrayList, DataTable Or Dataset
}
connection.Close();
}
catch (Exception exp)
{
lblMessage.Text=exp.Message;
}
}
}