我正在使用gridview,我想使用分页。我已经将allow paging设置为true,页面大小设置为5.我可以在gridview的基础上看到数字,但是当我点击一个数字移动到相应的页面时,它会抛出错误说:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
代码:
<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>
<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>
答案 0 :(得分:22)
您必须处理网格的 PageIndexChanging 事件
像
这样的东西protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Bind grid
}
答案 1 :(得分:5)
如果启用分页,则无效。您还需要为PageIndexChangeEvent编写事件处理程序。请检查此链接:http://forums.asp.net/post/1177923.aspx
答案 2 :(得分:1)
您需要编写“PageIndexChanging”事件以使其正常工作。为PageIndexChanging添加一个事件处理程序,你在其中设置GridView.CurrentPage = e.NewPage ...
答案 3 :(得分:1)
在HTML标记中再添加一个事件以进行分页。
OnPageIndexChanging="GridView1_PageIndexChanging"
现在从后面的代码处理相同的事件
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Your code
}