数据库绑定分页功能

创建脚本

<div style="filter:glow(color=#ffff00,strength=5,enabled=1)blendTrans(duration=1);FONT-SIZE:13px;HEIGHT:1px;"><strong>
                        <div style="font-size:12px;left: 201px; width: 448px; position:relative; top: -16px; height: 13px">
                            <asp:LinkButton ID="Lin_FirstPage" style="font-size:12px" runat="server" OnClick="Lin_FirstPage_Click">第一页</asp:LinkButton>&nbsp;
                            <asp:LinkButton ID="Lin_FrontPage" style="font-size:12px"  runat="server" OnClick="Lin_FrontPage_Click">上一页</asp:LinkButton>&nbsp;
                            <asp:LinkButton ID="Lin_NextPage" style="font-size:12px"  runat="server" OnClick="Lin_NextPage_Click">下一页</asp:LinkButton>&nbsp;
                            <asp:LinkButton ID="Lin_LastPage" style="font-size:12px"  runat="server" OnClick="Lin_LastPage_Click">最后一页</asp:LinkButton>&nbsp;
                            当前是第<asp:Label ID="Lab_CurrentPage" runat="server" style="font-size:12px"  Text="1" Width="7px"></asp:Label>页/共
                            <asp:Label ID="Lab_TotalPage" runat="server" Text="1" Width="10px"></asp:Label>页</div>
                    </strong></div>

分页绑定代码

 protected void Page_Load(object sender, EventArgs e)
    {
        RefreshDataGrid();
    }
    //分页绑定数据
    publicData F = new publicData();
    protected void RefreshDataGrid()
    {
        SqlConnection cnn = F.connection();
        cnn.Open();
        try
        {
            int iCurPages = Convert.ToInt32(Lab_CurrentPage.Text.Trim());
            PagedDataSource PDS_PageData = new PagedDataSource();
            DataSet mydataset = new DataSet();
            mydataset.Clear();
            string str1 = "select * from explainanswer";
            SqlDataAdapter sda = new SqlDataAdapter(str1, cnn);
            sda.Fill(mydataset, "judge");
            if (mydataset != null)
            {
                if (mydataset.Tables[0].Rows.Count != 0)
                {
                    PDS_PageData.DataSource = mydataset.Tables[0].DefaultView;
                    //允许分页
                    PDS_PageData.AllowPaging = true;
                    //每页最多显示10条内容
                    PDS_PageData.PageSize = 10;
                    //当前显示的页面是....数组下标
                    PDS_PageData.CurrentPageIndex = iCurPages - 1;
                    //对总页面的数量赋值
                    Lab_TotalPage.Text = Convert.ToString(PDS_PageData.PageCount);
                    //把分页后的数据集直接赋值
                    this.GridView1.DataSource = PDS_PageData;
                    //数据绑定并且显示
                    this.GridView1.DataBind();
                    ////设置列宽
                    //KDB_Select.DBConnectStop();
                }
            }
        }
        catch (Exception)
        {

        }
    }

    protected void Lin_FirstPage_Click(object sender, EventArgs e)
    {
        Lab_CurrentPage.Text = "1";
        RefreshDataGrid();
    }
    protected void Lin_FrontPage_Click(object sender, EventArgs e)
    {
        if (int.Parse(Lab_CurrentPage.Text.Trim()) - 1 != 0)
        {
            Lab_CurrentPage.Text = (int.Parse(Lab_CurrentPage.Text.Trim()) - 1).ToString();
            RefreshDataGrid();
        }
        else
        {
            Response.Write("<script language='javascript'>alert('已经是第一页!');</script>");
        }
    }
    protected void Lin_NextPage_Click(object sender, EventArgs e)
    {
        if ((int.Parse(Lab_CurrentPage.Text.Trim()) + 1) != (int.Parse(Lab_TotalPage.Text) + 1))
        {
            Lab_CurrentPage.Text = (int.Parse(Lab_CurrentPage.Text.Trim()) + 1).ToString();
            RefreshDataGrid();
        }
        else
        {
            Response.Write("<script language='javascript'>alert('已经是最后一页!');</script>");
        }
    }
    protected void Lin_LastPage_Click(object sender, EventArgs e)
    {
        Lab_CurrentPage.Text = Lab_TotalPage.Text;
        RefreshDataGrid();
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值