|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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 PagingGridView : System.Web.UI.Page
{
public int PageNum
{
get { return Convert.ToInt16(ViewState["PageNum"]); }
set { ViewState["PageNum"] = value; }
}
public int PageSize
{
get { return Convert.ToInt16(ViewState["PageSize"]); }
set { ViewState["PageSize"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PageNum = 1;
PageSize = 3;
BindGridView();
}
}
private string ConnectionString
{
get { return @"Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True"; }
}
private void BindGridView()
{
DataSet objDs = new DataSet();
System.Data.SqlClient.SqlConnection
myConnection = new SqlConnection(ConnectionString);
System.Data.SqlClient.SqlDataAdapter myCommand;
myCommand = new System.Data.SqlClient.SqlDataAdapter("sp_GridView_RowNumber", myConnection);
myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
// PageNum
myCommand.SelectCommand.Parameters.Add(new SqlParameter("@PageNum", SqlDbType.Int, 4));
myCommand.SelectCommand.Parameters["@PageNum"].Value = PageNum;
// PageSize
myCommand.SelectCommand.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
myCommand.SelectCommand.Parameters["@PageSize"].Value = PageSize;
// TotalRowsNum
SqlParameter TotalRowsNum = new SqlParameter("@TotalRowsNum", SqlDbType.Int);
TotalRowsNum.Value = null;
TotalRowsNum.Direction = ParameterDirection.Output;
myCommand.SelectCommand.Parameters.Add(TotalRowsNum);
myCommand.SelectCommand.Parameters["@TotalRowsNum"].Value = null;
myConnection.Open();
myCommand.Fill(objDs);
GridView1.DataSource = objDs;
GridView1.DataBind();
Navigation(Convert.ToInt32(TotalRowsNum.Value.ToString()));
}
private void Navigation(int totalRecords)
{
Double totalPages = Math.Ceiling(((double)totalRecords / PageSize));
if ((totalRecords == 1) || (totalPages == 0))
{
totalPages = 1;
}
if (PageSize > totalRecords)
{
PageSize = (int)totalPages;
}
GoToPageTxt.Text = PageNum.ToString();
CurrentPage.Text = PageNum.ToString();
TotalPages.Text = totalPages.ToString();
}
protected void NavigationLink_Click(Object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "First":
PageNum = 1;
break;
case "Last":
PageNum = Convert.ToInt16(TotalPages.Text);
break;
case "Next":
PageNum = Convert.ToInt16(CurrentPage.Text) + 1;
break;
case "Prev":
PageNum = Convert.ToInt16(CurrentPage.Text) - 1;
break;
}
BindGridView();
}
protected void PageSizeImb_Click(object sender, ImageClickEventArgs e)
{
PageNum = 1;
PageSize = Convert.ToInt16(PageSizeTxt.Text);
BindGridView();
}
protected void GoToPageImb_Click(object sender, ImageClickEventArgs e)
{
if (GoToPageTxt.Text != "")
{
int maxPage = Convert.ToInt32(TotalPages.Text);
int goToPage = Convert.ToInt32(GoToPageTxt.Text);
if (goToPage <= maxPage)
{
PageNum = goToPage;
BindGridView();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.