现在的位置: 首页 > 综合 > 正文

使用存储过程实现分页效果

2013年12月10日 ⁄ 综合 ⁄ 共 4620字 ⁄ 字号 评论关闭

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="4"
            ForeColor="#333333" GridLines="None" Width="167px" AllowPaging="True"

            PageSize="3">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="图片">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("photo") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <table class="style1">
                            <tr>
                                <td>
                                    姓名:<%# Eval("sname") %></td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("photo") %>'

                                        Width="16px" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    姓别:<%# Eval("sex")%>&nbsp;&nbsp;年龄:<%# Eval("age")%></td>
                            </tr>
                        </table>
                    </ItemTemplate>
                    <ControlStyle Height="80px" Width="60px" />
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerSettings Visible="False" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="&lt;" />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="&gt;" />
        <br />
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:HiddenField ID="HiddenField2" runat="server" />
    </div>
    </form>
</body>
</html>

后天代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindStudent(1);
    }
    private void BindStudent(int pageIndex)
    {
        string str = System.Configuration.ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
        using (SqlConnection sqlcnn = new SqlConnection(str))
        {
            SqlCommand sqlcmm = sqlcnn.CreateCommand();
            sqlcmm.CommandText = "sp_Student_Select_by_Page_rowNumber";
            sqlcmm.CommandType = System.Data.CommandType.StoredProcedure;
            sqlcmm.Parameters.AddWithValue("@pageSize", 3);
            sqlcmm.Parameters.Add("@pageCount", SqlDbType.Int).Direction =
                ParameterDirection.Output;
            sqlcmm.Parameters.AddWithValue("@pageIndex", pageIndex);
            SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
            this.HiddenField1.Value = pageIndex.ToString();
            this.HiddenField2.Value = sqlcmm.Parameters["@pageCount"].Value.ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.HiddenField1.Value);
        if (index > 1)
        {
            index--;
        }
        this.BindStudent(index);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.HiddenField1.Value);
        int total = Convert.ToInt32(this.HiddenField2.Value);
        if (index < total)
        {
            index++;
        }
        this.BindStudent(index);
    }
}

 

抱歉!评论已关闭.