Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am a student studying ASP.NET. I write this mail for some help.
I have some Questions about Custom Paging that the same with google paging.

For making this, I made some codes by C#. But I couldn't make progress after that code.

I've tried making that with Gridview, LinkButton, I got very Small Brain. So ...I coundn't... :((

What i made is this below.

public partial class _Default : System.Web.UI.Page
{
   private int iPage = 0;    //For count of the Tables
    private int pageSIZE = 20; //For the show of the record
    private int toPageCount;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindGridView(iPage, pageSIZE);
        }


    private int GetItemCount()
    {
        string connStr = "Data Source=;Initial  Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;";
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM AdventureWorks.Person.Contact", conn);   
        cmd.Connection.Open();
        int count = (int)cmd.ExecuteScalar();
        cmd.Connection.Close();
        return count;
    }

    private void BindGridView(int ipageIndex, int count)
    {
        string connstr = "Data Source=;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=";
        SqlConnection conn = new SqlConnection(connstr);
        string sqlcmd;
        sqlcmd = "SELECT TOP(" + count + ") * FROM AdventureWorks.Person.Contact " +
                    "WHERE ContactID NOT IN " +
                    "(SELECT TOP(" + (ipageIndex * count) + ") ContactID FROM AdventureWorks.Person.Contact ORDER BY ContactID DESC) " +
                    "ORDER BY ContactID DESC";
       
        SqlDataAdapter ad = new SqlDataAdapter(sqlcmd, conn);
        DataSet ds = new DataSet();
        ad.Fill(ds, "titles");
        GridView1.DataSource = ds.Tables["titles"];
        GridView1.DataBind();
    }

   }


So now, I have to make some buttons for paging, but it's very hard for me to make it.
Based on this code, I want to know how to make it like a google paging.
Could you tell me the better way that I make them?
Please help me.
Posted

1 solution

For the start I can give you some tips.
Use your GetItemCountMethod to determine how many paging links you need to generate

int linkCount = GetItemCount() / pageSize;


Use the above linkCount to generate the paging link and display them on the page wherever you want(mostly at the bottom of the gridview.
LinkButtton[] links = new LinkButton[linkCount]
for(int i = 0; i < linkCount; i++)
{
  links[i] = new LinkButton();
  link[i].Text = "Page " + (i + 1).ToString();
  link[i].ID = "Page " + (i + 1).ToString();
  //set link[i]'s other properties here
  somePlaceHolderOnYourPage.Controls.Add(link[i]);
  link[i].Click += GridViewPaging;
}


Now in the GridViewPaging event handler call the BindGridView with appropriate parameter.
public void GridViewPaging(object sender, EventArgs e)
{
  LinkButton link = (LinkButton)sender;
  int pageNumber = int.Parse(link.ID.SubString(4)); //if link.ID is "Page2", then get the 5th character which is 2.

  BindGridView(pageNumber, pageSize);
}


This code is just to give you an idea. This is not a tested code but should work with minor modifications.

Some tips: Use stored procedure rather then creating query in the code. This way you can avoid the GetItemCount method by passing the total number of records in the output parameter of the stored procedure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900