如何启用分页和排序在ASP.NET 4.0 Gri​​dView控件编程? [英] How to enable Paging and Sorting on ASP.NET 4.0 GridView programmatically?

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

问题描述

我使用ASP.NET 4.0与C#(的Visual Web Developer 2010防爆preSS)。

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

我已经成功地设法实现使用ASP.NET声明code绑定到存储过程数据源的简单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和使用code-背后(我试图把数据库访问code在一个地方)。到目前为止,我在$ C $有这样的C-背后:

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();
}    

我的声明code现在看起来像:

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 Gri​​dView控件编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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