ASP.NET - paging - Asked By ram s on 03-Jan-12 02:05 AM

 how to add paging index in a web page in c# language 
dipa ahuja replied to ram s on 03-Jan-12 02:16 AM

Here is the simple example of gridview paging:

Set the AllowPaging = True for gridview and write this code in codebehind

protected void Page_Load(object sender, EventArgs e)

{

   if (!IsPostBack)

   {

     bindGrid();

   }

}

void bindGrid()

{

   SqlDataAdapter da = new SqlDataAdapter("select * from TableName", "Connection String");

   DataTable dt = new DataTable();  

    da.Fill(dt);

   GridView1.DataSource = dt;

   GridView1.DataBind();

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

   GridView1.PageIndex = e.NewPageIndex;

   BindGrid();

   //Bind Grid

}

Riley K replied to ram s on 03-Jan-12 02:18 AM


You mean for gridview ??

Use the PageIndexChanging event of Gridview


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}

Regards
Jitendra Faye replied to ram s on 03-Jan-12 03:22 AM

For your solution check these things-


1. First Check

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
   {
    //bind your Grid here
   }
}

2. set AllowPaging Property of GridView to True.

3.  Write following code in PageIndexChanging() event of GridView-

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
 GridView1.PageIndex = e.NewPageIndex;
 //bind your Grid here
}

Follow this steps and let me know.



smr replied to ram s on 03-Jan-12 03:23 AM
hi

here is an example

private void Page_Load(object sender, System.EventArgs e)
{
  if ( !Page.IsPostBack )
  {
  BindData();
  }
  CreateLinks();
}
  
// The BindData method constructs a SQL query that uses nested SELECT TOP 
// statements (as described earlier) to retrieve a specified page of data.
//
public void BindData()
{
  SqlConnection myConnection = new SqlConnection(_connStr);
  String strCmd = "";
  StringBuilder sb = new StringBuilder();
  
  sb.Append("select top {0} CustomerID,CompanyName,ContactName,ContactTitle from (select top {1} CustomerID,CompanyName,ContactName,ContactTitle from Customers ");
  sb.Append("as t1 order by contactname desc) ");
  sb.Append("as t2 order by contactname asc");
  strCmd = sb.ToString(); 
  sb = null;
  
  // Set pseudoparameters: TableName, KeyField and RowIndex 
  strCmd = String.Format(strCmd, _pageSize, 
                 _currentPageNumber * _pageSize);
  
  // Prepare the command
  SqlCommand myCommand = new SqlCommand(strCmd,myConnection);
  
  SqlDataAdapter sa = new SqlDataAdapter(myCommand);
  DataSet searchData = new DataSet("SearchData");
  try
  {
  myConnection.Open();
  sa.Fill(searchData);
  
  MyDataGrid.DataSource = searchData;
  MyDataGrid.DataBind(); 
  }
  finally
  {
  myConnection.Close();
  }
  
  CurrentPage.Text = _currentPageNumber.ToString();
  if ( !Page.IsPostBack )
  {
  using (SqlConnection conn = new SqlConnection(_connStr))
  {
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT Count(*) FROM Customers";
    conn.Open();
    _totalRecords = (int)cmd.ExecuteScalar();
    _totalPages = _totalRecords / MyDataGrid.PageSize;
    TotalPages.Text = _totalPages.ToString();
  }
  }
  else
  {
  _totalPages = int.Parse ( TotalPages.Text ); 
  }
  
  if (_currentPageNumber == 1)
  {
  PreviousPage.Enabled = false;
  if (_totalPages > 1)
    NextPage.Enabled = true;
  else
  NextPage.Enabled = false;
  }
  else
  {
  PreviousPage.Enabled = true;
  if (_currentPageNumber == _totalPages)
    NextPage.Enabled = false;
  else
    NextPage.Enabled = true;
  }
}


refer
http://msdn.microsoft.com/en-us/library/ff650700.aspx
Sri K replied to ram s on 03-Jan-12 04:25 AM
Set the paging Index as given below
protected void gdvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
 {
e.NewPageIndex = e.NewPageIndex + 1;
DataBind();
 }


kalpana aparnathi replied to ram s on 03-Jan-12 05:53 AM
http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx

http://www.4guysfromrolla.com/articles/081804-1.aspx