2011-04-15 69 views
10

我正在使用gridview,我想使用分頁。我已經設置允許分頁爲true,頁面大小爲5.我可以在gridview的底部看到數字,但是當我點擊一個數字移動到各自的頁面時,它會拋出一個錯誤消息:'GridView1'觸發了未處理的事件PageIndexChanging

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> 
       &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> 
+3

您的大寫鎖定鍵被打破。 – EboMike 2011-04-15 04:44:01

+2

請勿輸入大寫字母。 – jonsca 2011-04-15 04:44:03

回答

22

你將不得不處理PageIndexChanging事件網格

喜歡的東西

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

} 
+1

感謝「//綁定網格」註釋,這意味着您應該編寫綁定網格的代碼,而不僅僅是「GridView1.DataBind();」 – 2013-07-31 04:58:33

1

你會需要編寫 「PageIndexChanging」 事件,使其工作。爲PageIndexChanging添加一個事件處理程序,您可以在其中設置GridView.CurrentPage = e.NewPage ...

1

再添加一個事件在HTML標記爲pagging。

OnPageIndexChanging="GridView1_PageIndexChanging" 

現在從代碼處理相同的事件背後

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    //Your code 
} 
相關問題