'GridView1' fired event PageIndexChanging which wasn't handled
Asked Answered
R

4

10

I am using a gridview and I want to use paging. I have already set allow paging to true and page size to 5. I can see the numbers at the base of my gridview, but when i click on a number to move to respective page, it throws an error saying:

The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.

Code:

 <asp:GridView ID="GridView1" runat="server" CellPadding="5" 
    AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="contact_id" 
    onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
    PageSize="5">
    <Columns>
        <asp:TemplateField HeaderText="contact_id">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("contact_id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="name">
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="address">
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("address") %>'></asp:Label><br />
                <asp:Label ID="Label6" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
                 <asp:Label ID="Label7" runat="server" Text='<%# Eval("state") %>'></asp:Label><br />
           <asp:Label ID="Label8" runat="server" Text='<%# Eval("pincode") %>'></asp:Label>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="email">
            <ItemTemplate>
                <asp:Label ID="Label9" runat="server" Text='<%# Eval("email") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="mobile">
            <ItemTemplate>
                <asp:Label ID="Label10" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="context">
            <ItemTemplate>
                <asp:Label ID="Label11" runat="server" Text='<%# Eval("context") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="status">
            <ItemTemplate>
                <asp:Label ID="Label12" runat="server" Text='<%# Eval("status") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>PENDING</asp:ListItem>
                <asp:ListItem>OK</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="Edit"></asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update"></asp:LinkButton>
                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemStyle CssClass="button" />
        </asp:TemplateField>
    </Columns>
    <PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
Rolo answered 15/4, 2011 at 4:41 Comment(1)
YOUR CAPS LOCK KEY IS BROKEN.Tyro
B
23

You will have to handle the PageIndexChanging event for the grid

Something like

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    //Bind grid

}
Bier answered 15/4, 2011 at 4:50 Comment(1)
Thanks for "//Bind grid" comment, which it means you should write the code which binds the grid not just "GridView1.DataBind();"Importation
C
5

It will not work if you enable paging. You also need to write the event handler for PageIndexChangeEvent. Check this link: http://forums.asp.net/post/1177923.aspx

Cavy answered 15/4, 2011 at 4:48 Comment(0)
A
1

You would need to code "PageIndexChanging" event to make it work. Add an event handler for the PageIndexChanging where you set the GridView.CurrentPage = e.NewPage...

Aarau answered 15/4, 2011 at 4:48 Comment(0)
F
1

Add one more event in HTML mark up for pagging.

OnPageIndexChanging="GridView1_PageIndexChanging"

Now handle same event from code behind

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Your code
}
Free answered 9/8, 2013 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.