多用途分页代码

 前台:

ExpandedBlockStart.gif代码
 <center>
                总页数:
<asp:Label ID="Label1" runat="server"></asp:Label>
                当前第:
<asp:Label ID="Label2" runat="server"></asp:Label>
                页
<span lang="zh-cn">&nbsp;&nbsp;&nbsp;&nbsp; 跳转到:第</span><asp:DropDownList 
                    ID
="DropDownList1" runat="server" AutoPostBack="True" 
                    onselectedindexchanged
="DropDownList1_SelectedIndexChanged">
                
</asp:DropDownList>
                
<span lang="zh-cn"></span><br />
                
<center>
                    
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="第一页" />
                    
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="上一页" />
                    
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" />
                    
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="最后一页" />
                
</center>
            
</center>

 

 

每次使用分页的时候 只用复制最下面两行 即可(可以不用修改控件名)!

 后台:

ExpandedBlockStart.gif代码
  protected void Page_Load(object sender, EventArgs e)
    {
        
if(!IsPostBack)
             bind();
    }

    
int pageCount;//总页数
    int currentPage = 1;//第定义当前页
    int count = 10;//每页条数

    
private void bind()
    {
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = GoodsManager.GetAllGoods().DefaultView;


        
//允许分页
        pds.AllowPaging = true;
        
//设置每页显示记录数
        pds.PageSize = count;
        
//获取总页数
        pageCount = pds.PageCount;
        
this.Label1.Text = pageCount.ToString();
        pds.CurrentPageIndex 
= currentPage - 1;
        
//当前页
        this.Label2.Text = Convert.ToString(currentPage);

        
//数据绑定 下拉列表

        this.RepeaterProductList.DataSource = pds;
        
this.RepeaterProductList.DataBind();

        DropDownList1.Items.Clear();
        
//绑定dropdownlist
        for (int i = 0; i < pageCount; i++)
        {
            DropDownList1.Items.Add((i+1).ToString());
        }
    }

    //截取字符
    public string SubstringStr(string str)
    {
        
if (str.Length > 10)
        {
            
return str.Substring(010);
        }
        
else
        {
            
return str;
        }
    }
  
  protected void RepeaterProductList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
//((LinkButton)e.Item.FindControl("LinkBtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')"); 


        
if (e.CommandName == "Delete")
        {
            
string idstr = e.CommandArgument.ToString();
            
int id = int.Parse(idstr);
            
int rs = GoodsManager.DeleteGood(id);
            
if (rs > 0)
            {
                
this.Page.ClientScript.RegisterStartupScript(this.GetType(), """<script>alert('删除成功!')</script>");
                bind();
            }
            
else
                
this.Page.ClientScript.RegisterStartupScript(this.GetType(), """<script>alert('删除失败!')</script>");
        }

    }
    
/// <summary>
    
/// 第一页
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
//如果当前不是第一页的时候
        if (this.Label2.Text == "1")
        {

        }
        
else
        {
            currentPage 
= 1;
            bind();
        }
    }
    
/// <summary>
    
/// 下一页
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
//如果当前不是最后页的时候
        if (this.Label1.Text == this.Label2.Text)
        {
        }
        
else
        {
            currentPage 
= int.Parse(this.Label2.Text) + 1;
            
this.Label2.Text = currentPage.ToString();
            bind();
        }
    }
    
/// <summary>
    
/// 上一页 
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
//如果当前不是第一页的时候
        if (this.Label2.Text != "1")
        {
            currentPage 
= int.Parse(this.Label2.Text) - 1;
            
this.Label2.Text = currentPage.ToString();
            bind();
        }
    }
    
/// <summary>
    
/// 最后一页
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Button4_Click(object sender, EventArgs e)
    {
//如果当前不是最后一页的时候
        if (this.Label1.Text != this.Label2.Text)
        {
            
this.Label2.Text = this.Label1.Text;
            currentPage 
= int.Parse(this.Label2.Text);
            bind();
        }
    }
    
//页面跳转

    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        currentPage 
= int.Parse(DropDownList1.SelectedItem.Text);
        bind();
       
// DropDownList1.Items.Clear();
    }

 

 灰色部分是需要更改的,其他部分可以直接拿来复用!

转载于:https://www.cnblogs.com/Simcoder/archive/2010/06/29/1767758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值