2011-02-01 93 views
3

我有一個簡單的查詢:分頁的GridView

Dim info As New SailMembersDataContext 
Dim query = From p In info.Individuals 
GridView1.DataSource = query 
GridView1.DataBind() 

,我想知道我怎麼可以在頁面我試圖使用內置的分頁的GridView控件上添加分頁來此查詢例如10但這只是產生錯誤:

GridView'GridView1'觸發了未處理的事件PageIndexChanging

試圖更改爲其他頁面時。

回答

3

好,很容易

手柄PageIndexChanging事件背後的代碼,

C#

void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e) 
{ 
    //For example 
    //Cancel the paging operation if the user attempts to navigate 
    //to another page while the GridView control is in edit mode. 
    if (GridView1.EditIndex != -1) 
    { 
     // Use the Cancel property to cancel the paging operation. 
     e.Cancel = true; 

     // Display an error message. 
     int newPageNumber = e.NewPageIndex + 1; 
     Message.Text = "Please update the record before moving to page " + 
     newPageNumber.ToString() + "."; 
    } 
    else 
    { 
     // Clear the error message. 
     Message.Text = ""; 
    } 
} 

VB.NET

Private Sub GridView1_PageIndexChanging(sender As [Object], e As GridViewPageEventArgs) 
    'For example 
    'Cancel the paging operation if the user attempts to navigate 
    'to another page while the GridView control is in edit mode. 
    If GridView1.EditIndex <> -1 Then 
     ' Use the Cancel property to cancel the paging operation. 
     e.Cancel = True 

     ' Display an error message. 
     Dim newPageNumber As Integer = e.NewPageIndex + 1 
     Message.Text = "Please update the record before moving to page " & newPageNumber.ToString() & "." 
    Else 
     ' Clear the error message. 
     Message.Text = "" 
    End If 
End Sub 

的d您的標記會是這樣的:

<asp:gridview id="GridView1" 
    autogeneratecolumns="true" 
    emptydatatext="No data available." 
    allowpaging="true" 
    autogenerateeditbutton="true" 
    onpageindexchanging="GridView1_PageIndexChanging" 
    runat="server"> 
    <pagersettings mode="Numeric" 
     position="Bottom"   
     pagebuttoncount="10"/> 

    <pagerstyle backcolor="LightBlue"/> 

    </asp:gridview> 
+0

謝謝你的幫忙!現在完美的工作! – Houlahan 2011-02-01 14:07:57

0

您需要處理事件的方法。

/// <summary> 
/// Handles the PageIndexChanging event. 
/// </summary> 
/// <param name="sender">The sender.</param> 
/// <param name="e">The instance containing the event data.</param>  
public static void GridViewPageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    [your application functionality here] 
}