如何在显示12个元素后启用GridView分页?

时间:2012-02-29 12:09:41

标签: c# asp.net

我是一名新的ASP.NET开发人员,现在我在系统中遇到了一个问题,我正在研究它。我有一个GridView,我已经启用了它的分页功能。我现在想要的是在GridView中显示12或14个元素,然后使用分页来显示其他12个元素,那么该怎么做呢?

我的ASP.NET代码:

<!-- Content Goes Here! -->
                                        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                                            DataSourceID="SqlDataSource6" CssClass="datatable"
                                            CellPadding="0" BorderWidth="0px" GridLines="None" 
                                        OnDataBinding="GridView3_DataBinding" AllowPaging="True">

                                            <Columns>
                                                <asp:BoundField DataField="Title" HeaderText="Quiz" 
                                                    SortExpression="Title" />
                                                <asp:BoundField DataField="DivisionShortcut" 
                                                    HeaderText="Division" 
                                                    SortExpression="DivisionShortcut" />
                                                <asp:BoundField DataField="Total Number of Participants" 
                                                    HeaderText="Total Number of Participants" ReadOnly="True" 
                                                    SortExpression="Total Number of Participants"/>
                                            </Columns>

                                            <RowStyle CssClass="row" />

                                            <SortedAscendingCellStyle CssClass="sortasc"></SortedAscendingCellStyle>
                                            <SortedAscendingHeaderStyle CssClass="sortasc"></SortedAscendingHeaderStyle>
                                            <SortedDescendingHeaderStyle CssClass="sortdesc"></SortedDescendingHeaderStyle>
                                        </asp:GridView>

                                        <asp:SqlDataSource ID="SqlDataSource6" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT     dbo.Quiz.Title, dbo.Divisions.DivisionShortcut,  COUNT(DISTINCT dbo.UserQuiz.Username) AS [Total Number of Participants]
        FROM         dbo.employee INNER JOIN
                              dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username INNER JOIN
                              dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID INNER JOIN
                              dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
        GROUP BY dbo.Quiz.Title, dbo.Divisions.DivisionShortcut
        ORDER BY dbo.Quiz.Title"></asp:SqlDataSource>

那怎么做?

3 个答案:

答案 0 :(得分:2)

AllowPaging="True" PageSize="12"

答案 1 :(得分:1)

<!-- Content Goes Here! -->
                                    <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                                        DataSourceID="SqlDataSource6" CssClass="datatable"
                                        CellPadding="0" BorderWidth="0px" GridLines="None" 
                                    OnDataBinding="GridView3_DataBinding" AllowPaging="True" PageSize="12">

                                        <Columns>
                                            <asp:BoundField DataField="Title" HeaderText="Quiz" 
                                                SortExpression="Title" />
                                            <asp:BoundField DataField="DivisionShortcut" 
                                                HeaderText="Division" 
                                                SortExpression="DivisionShortcut" />
                                            <asp:BoundField DataField="Total Number of Participants" 
                                                HeaderText="Total Number of Participants" ReadOnly="True" 
                                                SortExpression="Total Number of Participants"/>
                                        </Columns>

                                        <RowStyle CssClass="row" />

                                        <SortedAscendingCellStyle CssClass="sortasc"></SortedAscendingCellStyle>
                                        <SortedAscendingHeaderStyle CssClass="sortasc"></SortedAscendingHeaderStyle>
                                        <SortedDescendingHeaderStyle CssClass="sortdesc"></SortedDescendingHeaderStyle>
                                    </asp:GridView>

                                    <asp:SqlDataSource ID="SqlDataSource6" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT     dbo.Quiz.Title, dbo.Divisions.DivisionShortcut,  COUNT(DISTINCT dbo.UserQuiz.Username) AS [Total Number of Participants]
    FROM         dbo.employee INNER JOIN
                          dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username INNER JOIN
                          dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID INNER JOIN
                          dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode
    GROUP BY dbo.Quiz.Title, dbo.Divisions.DivisionShortcut
    ORDER BY dbo.Quiz.Title"></asp:SqlDataSource>

答案 2 :(得分:1)

在asp:gridview

中添加以下属性
 OnPageIndexChanging="grdview_PageIndexChanging" PageSize="12" AllowPaging="True"

然后在CodeBehind中添加以下功能

  protected void grdeview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // your gridview ID 
        GridView3.PageIndex = e.NewPageIndex;
        GridView3.Datasource = your datasource ;
        GridView3.DataBind();
        // or your can create separate function for bindGridview. and than call it directly here.  
    }