开发者

Asp.Net: Gridview OnPageIndexChanging not firing server method

I am not able to change the page index of the gridview. The server method for OnPageIndexChanging is not at all getting fired. I do not know what am I doing wrong here.

Here's my 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>

My server side code is as follows,

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 = CurrentSchoolD开发者_如何学JAVAetails.PageViewCount;
    IList<Comment> allComments = CommentRepository.GetAllCommentsByVideoID(VideoID);
    BindDataControls.BindGridView(VideoCommentsGrid, allComments);
}

Please help me out, Thanks.


I think this is occurring because you are binding the data to the GridView on every page load, where as you only need to bind the data once initially and then again when the page index changes.

Try changing the code in the Page_Load method like so:

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


on the rowdatabound method try to comment this piece of code and see what happen.

//if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜