标题:datalist实现类似百度分页(有点不明显的小毛病&&等待解决)
只看楼主
雪雨星风
Rank: 1
来 自:北京
等 级:新手上路
威 望:1
帖 子:566
专家分:0
注 册:2007-10-11
结帖率:100%
 问题点数:0 回复次数:16 
datalist实现类似百度分页(有点不明显的小毛病&&等待解决)
using System;
using System.Data;
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;
using System.Data.SqlClient;
public partial class bbs_photo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {     
            publicclass.pageintex = 0;
            datelistbind();            
        }
    }
    protected void datelistbind()
    {
         
        string sqlcomm = "select * from UF_PICTURE";
        SqlConnection myconn = new SqlConnection(publicclass.getconnstr());
        myconn.Open();
        SqlDataAdapter sdr = new SqlDataAdapter();
        DataSet ds = new DataSet();
        sdr.SelectCommand = new SqlCommand(sqlcomm, myconn);
        sdr.Fill(ds, "UF_PICTURE");
        PagedDataSource pds = new PagedDataSource();        //分页数据源
        pds.DataSource = ds.Tables["UF_PICTURE"].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 4;
        pds.CurrentPageIndex = publicclass.pageintex;
        Response.Write(publicclass.pageintex);
        publicclass.count = pds.PageCount;
        DataList1.DataSource = pds;
        DataList1.DataBind();
        Response.Write(publicclass.count);
    }     
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            Control objectcontorl5 = e.Item.FindControl("LinkButton5");
            Control objectcontorl6 = e.Item.FindControl("LinkButton6");
            Control objectcontorl9 = e.Item.FindControl("LinkButton9");
            Control objectcontorl10 = e.Item.FindControl("LinkButton10");
            Control objectcontorl11 = e.Item.FindControl("LinkButton11");
            Control objectcontorl7 = e.Item.FindControl("LinkButton7");
            Control objectcontorl8 = e.Item.FindControl("LinkButton8");      
            LinkButton bt5 = (LinkButton)objectcontorl5;
            LinkButton bt6 = (LinkButton)objectcontorl6;
            LinkButton bt9 = (LinkButton)objectcontorl9;
            LinkButton bt10 = (LinkButton)objectcontorl10;
            LinkButton bt11 = (LinkButton)objectcontorl11;
            LinkButton bt7 = (LinkButton)objectcontorl7;
            LinkButton bt8 = (LinkButton)objectcontorl8;         
            publicclass.yi1 = Convert.ToInt32(bt9.Text) -1;
            publicclass.er1 = Convert.ToInt32(bt10.Text) -1;
            publicclass.san1 = Convert.ToInt32(bt11.Text) -1;           
            if (publicclass.pageintex <= 1)
            {
                bt9.Text = "1";
                bt10.Text = "2";
                bt11.Text = "3";
            }
            else if (publicclass.pageintex >= publicclass.count - 2)
            {
                bt9.Text = Convert.ToString(publicclass.count - 2);
                bt10.Text = Convert.ToString(publicclass.count - 1);
                bt11.Text = Convert.ToString(publicclass.count);
            }
            else
            {
                bt9.Text = Convert.ToString(publicclass.pageintex - 1);
                bt10.Text = Convert.ToString(publicclass.pageintex);
                bt11.Text = Convert.ToString(publicclass.pageintex + 1);
            }
            if (publicclass.pageintex == 0)
            {
                bt5.Enabled = false;
                bt6.Enabled = false;               
            }           
            if (publicclass.pageintex == publicclass.count)
            {
                bt7.Enabled = false;
                bt8.Enabled = false;               
            }     
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {         
            Control objectcontorl12 = e.Item.FindControl("LinkButton12");
            Control objectcontorl13 = e.Item.FindControl("LinkButton13");
            Control objectcontorl14 = e.Item.FindControl("LinkButton14");
            Control objectcontorl15 = e.Item.FindControl("LinkButton15");
            Control objectcontorl16 = e.Item.FindControl("LinkButton16");
            Control objectcontorl17 = e.Item.FindControl("LinkButton17");
            Control objectcontorl18 = e.Item.FindControl("LinkButton18");            
            LinkButton bt12 = (LinkButton)objectcontorl12;
            LinkButton bt13 = (LinkButton)objectcontorl13;
            LinkButton bt14 = (LinkButton)objectcontorl14;
            LinkButton bt15 = (LinkButton)objectcontorl15;
            LinkButton bt16 = (LinkButton)objectcontorl16;
            LinkButton bt17 = (LinkButton)objectcontorl17;
            LinkButton bt18 = (LinkButton)objectcontorl18;           
            publicclass.yi2 = Convert.ToInt32(bt14.Text) - 1;
            publicclass.er2 = Convert.ToInt32(bt15.Text) - 1;
            publicclass.san2 = Convert.ToInt32(bt16.Text) - 1;
            if (publicclass.pageintex <= 1)
            {
                bt14.Text = "1";
                bt15.Text = "2";
                bt16.Text = "3";
            }
            else if (publicclass.pageintex >= publicclass.count - 2)
            {
                bt14.Text = Convert.ToString(publicclass.count - 2);
                bt15.Text = Convert.ToString(publicclass.count - 1);
                bt16.Text = Convert.ToString(publicclass.count);
            }
            else
            {
                bt14.Text = Convert.ToString(publicclass.pageintex - 1);
                bt15.Text = Convert.ToString(publicclass.pageintex);
                bt16.Text = Convert.ToString(publicclass.pageintex + 1);
            }
            if (publicclass.pageintex == 0)
            {
                bt12.Enabled = false;
                bt13.Enabled = false;                 
            }
            if (publicclass.pageintex == publicclass.count)
            {
                bt17.Enabled = false;
                bt18.Enabled = false;                 
            }     
        }
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = 0;
        datelistbind();
    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {      
        publicclass.pageintex = 0;
        datelistbind();      
    }
    protected void LinkButton9_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = Convert.ToInt32(publicclass.yi1);
        datelistbind();
    }
    protected void LinkButton10_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = Convert.ToInt32(publicclass.er1);
        datelistbind();
    }
    protected void LinkButton11_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = Convert.ToInt32(publicclass.san1);         
        datelistbind();
    }
    protected void LinkButton7_Click(object sender, EventArgs e)
    {      
        publicclass.pageintex = publicclass.pageintex + 1;
        datelistbind();      
    }
    protected void LinkButton8_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = publicclass.count;
        datelistbind();
    }
    protected void LinkButton12_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = 0;
        datelistbind();
    }
    protected void LinkButton13_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = publicclass.pageintex - 1;
        datelistbind();
    }
    protected void LinkButton14_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = Convert.ToInt32(publicclass.yi2);
        datelistbind();
    }
    protected void LinkButton15_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = Convert.ToInt32(publicclass.er2);
        datelistbind();
    }
    protected void LinkButton16_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = Convert.ToInt32(publicclass.san2);
        datelistbind();
    }
    protected void LinkButton17_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = publicclass.pageintex + 1;
        datelistbind();
    }
    protected void LinkButton18_Click(object sender, EventArgs e)
    {
        publicclass.pageintex = publicclass.count;
        datelistbind();
    }
}
//前台代码
--------------------------------------------------------
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" RepeatColumns="2">
        <ItemTemplate>
<div class="right_tu_1 bor_r">
<h3>效果图12</h3>
<span> <asp:Image ID="Image1" runat="server" Height="160px" Width="258px" ImageUrl="~/"/><%# Eval("UF_PICTURE_ADDRESS")%><label>上 传 者:<asp:LinkButton ID="LinkButton2" runat="server"><%# Eval("UF_PICTURE_USER")%></asp:LinkButton>

    上传时间:<asp:LinkButton ID="LinkButton3" runat="server"><%# Eval("UF_PICTURE_TIME")%></asp:LinkButton>
     
所属分类:<asp:LinkButton ID="LinkButton4" runat="server"><%# Eval("UF_PICTURE_TYPE")%></asp:LinkButton><a href="#"></a>

<a href="#" class="sq">
    <asp:LinkButton ID="LinkButton1" runat="server">进入评论</asp:LinkButton></a>点击次数:<%# Eval("UF_PICTURE_VISITQUANTITY")%> 次</label>
</span>
</div>
        </ItemTemplate>
        <HeaderTemplate>
            <div class="fenye">
                <label>
                    <asp:LinkButton ID="LinkButton20" runat="server" BackColor="Transparent" BorderColor="Transparent"
                        BorderStyle="None">上传图片</asp:LinkButton><a href="#"></a></label>
                <span style="width: 429px">共有<strong>28</strong>张图片 共3页
                    <asp:LinkButton ID="LinkButton5" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">首页</asp:LinkButton><a href="#"></a>
                    <asp:LinkButton ID="LinkButton6" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">< 上一页</asp:LinkButton><a href="#"> </a>
                    <asp:LinkButton ID="LinkButton9" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">1</asp:LinkButton><a href="#"></a>
                    <asp:LinkButton ID="LinkButton10" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">2</asp:LinkButton><a href="#"></a>
                    <asp:LinkButton ID="LinkButton11" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">3</asp:LinkButton>
                    <asp:LinkButton ID="LinkButton7" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">下一页 > </asp:LinkButton>
                    <asp:LinkButton ID="LinkButton8" runat="server" Width="24px" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">尾页</asp:LinkButton><a href="#"></a></span></div>
        </HeaderTemplate>
        <FooterTemplate>
            <div class="fenye" id="fenye" style="width: 877px">
                <label>
                    <asp:LinkButton ID="LinkButton19" runat="server" BackColor="Transparent" BorderColor="Transparent"
                        BorderStyle="None">上传图片</asp:LinkButton><a href="#"></a></label>
                <span style="width: 416px">共有<strong>28</strong>张图片 共<strong>3</strong>页
                    <asp:LinkButton ID="LinkButton12" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">首页</asp:LinkButton><a href="#"></a>
                    <asp:LinkButton ID="LinkButton13" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">< 上一页</asp:LinkButton><a href="#">
                    </a>
                    <asp:LinkButton ID="LinkButton14" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">1</asp:LinkButton><a href="#"></a>
                    <asp:LinkButton ID="LinkButton15" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">2</asp:LinkButton>
                    <asp:LinkButton ID="LinkButton16" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">3</asp:LinkButton>
                    <asp:LinkButton ID="LinkButton17" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">上一页 > </asp:LinkButton>
                    <asp:LinkButton ID="LinkButton18" runat="server" BackColor="Transparent" BorderColor="Transparent" BorderStyle="None">尾页</asp:LinkButton><a href="#"></a></span></div>
        </FooterTemplate>
    </asp:DataList>" border="0" />
搜索更多相关主题的帖子: 百度 datalist 毛病 
2008-07-01 15:22
雪雨星风
Rank: 1
来 自:北京
等 级:新手上路
威 望:1
帖 子:566
专家分:0
注 册:2007-10-11
得分:0 
效果如下
首页  上一页  1 2 3  下一页  尾页
这个代码有问题   但我找不到是那里的错误  点击2时还正常   点击3或1时没效果   希望看到的高人能给予指点

C#发烧友QQ群1:5953354   QQ群2:68096127  QQ群3:30217249  QQ群4:37023079   为了更好方便大家交流学习,请不要重复加群
2008-07-01 15:22
雪雨星风
Rank: 1
来 自:北京
等 级:新手上路
威 望:1
帖 子:566
专家分:0
注 册:2007-10-11
得分:0 
类里的代码
public static int  yi1, er1, san1,yi2, er2,san2,count,pageintex;

C#发烧友QQ群1:5953354   QQ群2:68096127  QQ群3:30217249  QQ群4:37023079   为了更好方便大家交流学习,请不要重复加群
2008-07-01 15:25
tomtory
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:705
专家分:0
注 册:2008-6-12
得分:0 
我晕你哦

这种不是大错啊  自己认真找一下噻

要习惯于 打断点  监视  等方法来查找错误

这样你也或获得不少好处的嘛
2008-07-01 15:31
雪雨星风
Rank: 1
来 自:北京
等 级:新手上路
威 望:1
帖 子:566
专家分:0
注 册:2007-10-11
得分:0 
断点打不出来  代码怎么会不运行哪?

C#发烧友QQ群1:5953354   QQ群2:68096127  QQ群3:30217249  QQ群4:37023079   为了更好方便大家交流学习,请不要重复加群
2008-07-01 15:58
tomtory
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:705
专家分:0
注 册:2008-6-12
得分:0 
我晕  怎么可能的哦  打的断点要打到相应的位置噻
打到if (e.Item.ItemType == ListItemType.Header) 这上面
2008-07-01 16:20
雪雨星风
Rank: 1
来 自:北京
等 级:新手上路
威 望:1
帖 子:566
专家分:0
注 册:2007-10-11
得分:0 
断点没反应是不是没运行代码啊

C#发烧友QQ群1:5953354   QQ群2:68096127  QQ群3:30217249  QQ群4:37023079   为了更好方便大家交流学习,请不要重复加群
2008-07-01 16:49
tomtory
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:705
专家分:0
注 册:2008-6-12
得分:0 
我晕 你是怎么运行的哦!??

按F5运行

你是不是在页面那点点击右键运行的嘛!!那样是不能打到断点的噻

要按F5运行 或是点击上面那个绿色的箭头符号也可以

晕你的哦

还有 顺便说一下  在运行状态是不可以编辑代码的  只有关闭了这个运行转台才能编辑代码
2008-07-01 17:02
蓝色畅想
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-6-30
得分:0 
我的看法
首先这个 效果我以前作过 已经实现了

在思路上 我和你不同

1. 在数据库中 用一个表
    create table id
    (
      id int not null
    )
    循环存储 你要显示 分页的总页数

2.  再用一个DataList  把这这个表的 id 绑定到 要分页的 DataList 的下面
    注意: 在绑定时 你的SQL 语句要写成 每次查询你想要的数目 比如 10条
    1 2 3 4 5 6 7 8 9 10

3.  然后点击 下面的数字 把它传给你上面 用于控制分页 的Label; 我想 你有这个思路
    应该 能写的出来了啊!
2008-07-01 17:05
tomtory
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:705
专家分:0
注 册:2008-6-12
得分:0 
如果ID为自增长的话  哪么你删除一条数据  比如说 1、2、3、4、
现在我吧3删除表,哪么这里就会缺省了!!
所以绑直接ID不可用

可以数总数 然后把这个总数遍历成1、2、3、....然后将ID存到它对应的其它属性上可以考虑这样写一下
2008-07-01 17:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-222118-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.638829 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved