如何以编程方式在 ASP.NET 4.0 GridView 上启用分页和排序? [英] How to enable Paging and Sorting on ASP.NET 4.0 GridView programmatically?

查看:19
本文介绍了如何以编程方式在 ASP.NET 4.0 GridView 上启用分页和排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET 4.0 和 C#(Visual Web Developer 2010 Express).

I am using ASP.NET 4.0 with C# (Visual Web Developer 2010 Express).

我已经成功地使用声明性 ASP.NET 代码实现了一个绑定到存储过程数据源的简单 GridView,如下所示:

I have successfully managed to implement a simple GridView bound to a stored procedure data source using declarative ASP.NET code as shown here:

<asp:GridView 
    ID="grdTrades" 
    runat="server" 
    DataKeyNames="tradeId" 
    EnablePersistedSelection="true"
    SelectedRowStyle-BackColor="Yellow" 
    AllowPaging="true" 
    AllowSorting="true"
    PageSize = "20" 
    AutoGenerateColumns="false" 
    DataSourceID="sdsTrades" 
    >
    <Columns>
        <asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" />
        <asp:BoundField DataField="tradeId" HeaderText="TradeId"  ReadOnly="True" SortExpression="tradeId" />
        < ... more columns ... >
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="sdsTrades" runat="server" 
    ConnectionString="<%$ ConnectionStrings:TradesDB %>" 
    ProviderName="<%$ ConnectionStrings:Trades.ProviderName %>"  
    SelectCommand="usp_GetTrades" SelectCommandType="StoredProcedure">      
</asp:SqlDataSource>

它非常适用,包括分页和排序.我想删除 SqlDataSource 并使用代码隐藏(我试图将数据库访问代码放在一个地方).到目前为止,我的代码隐藏中有这个:

It works great including paging and sorting. I want to remove the SqlDataSource and use code-behind (I'm trying to put database access code in one place). So far I have this in my code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        grdTrades.SelectedIndex = 0;
        DBUtil DB = new DBUtil();
        grdTrades.DataSource = DB.GetTrades();
        grdTrades.DataKeyNames = new string[] { "tradeId" };
        grdTrades.DataBind();            
    }
}

// this is needed otherwise I get "The GridView 'grdTrades' fired event PageIndexChanging which wasn't handled."
void grdTrades_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
    grdTrades.PageIndex = e.NewPageIndex;
    grdTrades.DataBind();
}    

我的声明性代码现在看起来像:

My declarative code now looks like:

<asp:GridView 
    ID="grdTrades" 
    runat="server" 
    EnablePersistedSelection="true"            
    SelectedRowStyle-BackColor="Yellow" 
    AllowPaging="true" 
    AllowSorting="true"
    PageSize = "20" 
    AutoGenerateColumns="false" 

    OnPageIndexChanging="grdTrades_PageIndexChanging"
    >
    <Columns>
        <asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" />
        <asp:BoundField DataField="tradeId" HeaderText="TradeId"  ReadOnly="True" SortExpression="tradeId" />
        < ... more columns ... >           
    </Columns>
</asp:GridView>

问题是当我点击页码时,页面变成空白.我也想实现排序,但想让分页先工作.请帮忙.

The problem is when I click on a page number the page becomes blank. I would also like to implement sorting but would like to get the paging working first. Please help.

谢谢

推荐答案

每次换页时都需要绑定 GridView.

You need to bind your GridView every time you change page.

例如:

void grdTrades_PageIndexChanging(Object sender, GridViewPageEventArgs e) 
{
    grdTrades.DataSource = DB.GetTrades();  
    grdTrades.PageIndex = e.NewPageIndex; 
    grdTrades.DataBind(); 
} 

我的建议是将来自 DB.GetTrades() 的结果存储在 ViewState(或缓存)中,这样您就无需在每次更改页面时都访问数据库.

My advice would be to store your results from DB.GetTrades() in the ViewState (or Cache) so you don't need to go to the database everytime you change page.

不过,这样做时排序会变得非常困难.

Sorting can become quite difficult when doing this, though.

您始终可以使用 ObjectDataSource 而不是 SqlDatasource.然后您可以指向您的 ObjectDataSource 以查看您的 DB.GetTrades() 函数.排序和分页将自动工作.

You can always use an ObjectDataSource instead of a SqlDatasource. You can then point your ObjectDataSource to look at your DB.GetTrades() function. Sorting and Paging will work automatically.

希望有所帮助.

这篇关于如何以编程方式在 ASP.NET 4.0 GridView 上启用分页和排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆