关于自定义分页... PLZ帮帮我~~ [英] About Custom Paging...PLZ Help me~~

查看:98
本文介绍了关于自定义分页... PLZ帮帮我~~的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我是一名学习ASP.NET的学生.我写这封邮件寻求帮助.
我有一些关于自定义分页的问题,与Google分页一样.

为此,我用C#编写了一些代码.但是在该代码之后我无法取得进展.

我已经尝试使用Gridview,LinkBut​​ton做到这一点,我的大脑很小.所以...我感到...:((<
我在下面做的是这个.

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();
    }

   }



所以现在,我必须制作一些用于分页的按钮,但是这对我来说很难.
基于此代码,我想知道如何使其像Google页面一样.
你能告诉我我制作它们的更好方法吗?
请帮助我.



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.

推荐答案

首先,我可以给您一些提示.
使用GetItemCountMethod确定需要生成多少个页面链接.

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;



使用上面的linkCount生成分页链接,并将它们显示在页面上所需的任何位置(通常在gridview的底部.



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;
}



现在,在GridViewPaging事件处理程序中,使用适当的参数调用BindGridView.



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);
}



这段代码只是为了给您一个想法.这不是经过测试的代码,但应进行较小的修改.

一些提示:使用存储过程,而不是在代码中创建查询.这样,您可以通过在存储过程的输出参数中传递记录总数来避免GetItemCount方法.



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.


这篇关于关于自定义分页... PLZ帮帮我~~的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆