none
在GridView中用存储过程不显示页码的问题 RRS feed

  • 问题

  • 先看后台代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
    GridViewBind();
        }
    }
    private uint pageCount;  //总页数  
    private uint recordCount;  //总记录数 
    
    protected void GridViewBind()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString1"]);
        conn.Open();
        SqlCommand command = new SqlCommand("SQL_GetDataPage_AwCollection", conn);  //第一个参数为存储过程名  
        command.CommandType = CommandType.StoredProcedure;   //声明命令类型为存储过程  
        command.Parameters.Add("@pageSize", SqlDbType.Int);
        command.Parameters["@pageSize"].Value = GridView1.PageSize;
        command.Parameters.Add("@pageIndex", SqlDbType.Int);
        command.Parameters["@pageIndex"].Value = GridView1.PageIndex;
        command.Parameters.Add("@pageCount", SqlDbType.Int);
        command.Parameters["@pageCount"].Value = pageCount;
        command.Parameters["@pageCount"].Direction = ParameterDirection.Output;  //存储过程中的输出参数  
        command.Parameters.Add("@recordCount", SqlDbType.Int);
        command.Parameters["@recordCount"].Value = recordCount;
        command.Parameters["@recordCount"].Direction = ParameterDirection.Output; //存储过程中的输出参数  
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "Table1");
        //获得输出参数值  
        pageCount = Convert.ToUInt32(command.Parameters["@pageCount"].Value);
        recordCount = Convert.ToUInt32(command.Parameters["@recordCount"].Value);
        this.GridView1.DataSource = ds.Tables["Table1"];
        this.GridView1.DataBind();
        conn.Close();
    }
    存储过程是这样的

    --@PageSize:分页大小,PageIndex:页号,@PageCount:总页数,@recordCount:记录数
    CREATE PROCEDURE SQL_GetDataPage_AwCollection @pageSize int, @pageIndex int, @pageCount int output, @recordCount int output AS
    declare @SQL varchar(2000)
    select @recordCount=count(*) from SQL_AwCollection
    set @pageCount=ceiling(@recordCount*1.0/@pageSize)
    if @pageIndex = 0 or @pageCount<=1
    set @SQL='select top '+str(@pageSize)+' * from SQL_AwCollection order by aid desc'
    else if @pageIndex = @pageCount -1
     set @SQL='select * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' * from SQL_AwCollection order by aid asc) TempTable order by aid desc'
    else  set @SQL='select top '+str(@pageSize) +' * from ( select top '+str(@recordCount - @pageSize * @pageIndex)+' * from SQL_AwCollection order by aid asc) TempTable order by aid desc' 
    exec(@SQL)
    GO
    前台aspx页面的GridView是这样的

    <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="5" CellSpacing="1"
    ForeColor="Black" BorderWidth="1px" BorderColor="Tan" BackColor="LightGoldenrodYellow"
    AutoGenerateColumns="False" AllowPaging="True" OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
    DataKeyNames="ArtworkPicName,aid" OnPageIndexChanging="GridView1_PageIndexChanging"
    DataMember="aid" GridLines="None" PageSize="10">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="aid" ReadOnly="True" HeaderStyle-Width="30px"
    HeaderStyle-HorizontalAlign="Left" />
    ……
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" HeaderStyle-Width="80px"
    HeaderStyle-HorizontalAlign="Left" />
    </Columns>
    <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText=""
        Mode="Numeric" />
    <RowStyle Height="25px" />
    <EditRowStyle BackColor="#F1F1F1" />
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
        </asp:GridView>

    问题说明:正常情况下,以上程序应该每页显示10条记录,页脚区有1234的页码,点击可翻页。但我的上面的程序运行时仅仅能显示第一页的10条记录,页脚部分不显示页码,连“1”也不显示。

    我尝试不用存储过程的写法,是可以正常的,页脚区能显示页面,代码如下:

    protected void GridViewBind()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString1"]);
        SqlDataAdapter ad = new SqlDataAdapter("select * from SQL_AwCollection order by aid desc", conn);
        DataSet dst = new DataSet();
        ad.Fill(dst, "Table1");
        conn.Open();
        this.GridView1.DataSource = dst.Tables["Table1"];
        this.GridView1.DataBind();
        conn.Close();
    }
    请问一下,问题出在哪里,能帮我解决我感激不尽!!!
    2009年12月10日 6:30

全部回复