Asp.Net:Gridview OnPageIndexChanging没有触发服务器方法

时间:2011-10-06 13:19:50

标签: asp.net gridview aspxgridview

我无法更改gridview的页面索引。 OnPageIndexChanging 的服务器方法根本没有被触发。我不知道我在这里做错了什么。

这是我的gridview

<asp:GridView ID="VideoCommentsGrid" runat="server" 
                        OnRowDataBound="VideoCommentsGrid_RowDataBound" 
                        OnPageIndexChanging="VideoCommentsGrid_PageIndexChanging" allowpaging="true" 
                        CssClass="tables" 
                        EmptyDataText="<div class='notice show bottom'>No Comments found.</div>" 
                        AutoGenerateColumns="False" >
                <Columns>
                    <asp:TemplateField HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="70" ItemStyle-HorizontalAlign="center">
                        <HeaderTemplate>
                            Approve
                            <br />
                            <input id="ChkAllApprovedItems"  type="checkbox" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkApproval" Checked='<%#Eval("IsApproved").ToString()=="1"?true:false %>'  runat="server"  />
                            <asp:Label ID="lblCommentID" runat="server" Text='<%#Eval("CommentId") %>' CssClass="hide"/>
                         </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>

                     <asp:TemplateField HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="70" ItemStyle-HorizontalAlign="center">
                        <HeaderTemplate>
                            Reject
                            <br />
                            <input id="ChkAllRejectedItems"  type="checkbox" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkReject" Checked='<%#Eval("IsRejected").ToString()=="1"?true:false %>'  runat="server"  />
                         </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="User Name" HeaderStyle-Width="70" >
                        <ItemTemplate>
                            <asp:Label ID="lblUserName" runat="server" Text='<%#Eval("FirstName")%>'>  </asp:Label>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" Width="150" />
                        <ItemStyle HorizontalAlign="Left" CssClass="wordWrap" Width="150" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Comments" HeaderStyle-Width="70" >
                        <ItemTemplate>
                            <asp:Label ID="lblComment" runat="server" Text='<%#Eval("VideoComment") %>' />
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" Width="150" />
                        <ItemStyle HorizontalAlign="Left" CssClass="wordWrap" Width="150" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Comment Time* ">
                        <ItemTemplate>
                            <asp:Label ID="lblCommentDate" runat="server" Text='<%#(Eval("CommentCreatedDate"))%>'> </asp:Label>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" Width="80" />
                        <ItemStyle HorizontalAlign="Center" Width="80" />
                    </asp:TemplateField>

                </Columns>
                <HeaderStyle Height="30" />
                <PagerStyle HorizontalAlign="Center" CssClass="footer" />
                <AlternatingRowStyle CssClass="odd" />

            </asp:GridView>

我的服务器端代码如下,

protected void Page_Load(object sender, EventArgs e)
{
        LoadCommentsGridView(VideoCommentsGrid.PageIndex);
}

protected void VideoCommentsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;

}
protected void VideoCommentsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    VideoCommentsGrid.PageIndex = e.NewPageIndex;
    LoadCommentsGridView(VideoCommentsGrid.PageIndex);
    hidCheckedValue.Value = string.Empty;
}
protected void LoadCommentsGridView(int PageIndex)
{
    SetPageIndex(PageIndex);
    LoadDefaultGrid();
}
private void LoadDefaultGrid()
{
    VideoCommentsGrid.PageSize = CurrentSchoolDetails.PageViewCount;
    IList<Comment> allComments = CommentRepository.GetAllCommentsByVideoID(VideoID);
    BindDataControls.BindGridView(VideoCommentsGrid, allComments);
}

请帮帮我, 感谢。

2 个答案:

答案 0 :(得分:0)

<{1>}方法上的

尝试对这段代码进行评论,看看会发生什么。

rowdatabound

答案 1 :(得分:0)

我认为这是因为您在每次页面加载时将数据绑定到GridView,因为您只需要在初始时绑定数据,然后在页面索引更改时再次绑定数据。

尝试更改Page_Load方法中的代码,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        LoadCommentsGridView(VideoCommentsGrid.PageIndex);
    }
}