2011-03-21 75 views
2

這似乎是一個容易提出的問題,但我無法在GridView中顯示項目。這裏是我的代碼:GridView PageIndexChanging不工作

public partial class TestList : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!Page.IsPostBack) 
     { TestProject.DataAccess.Repository.Instance.Initialize(Settings.Default.TestConnection); 
      BindData(); 
     } 

    } 

    private void BindData() 
    { 
     //Restriction Info!! 
     gvAgentList.DataSource = EntityRegistration.DataAccess.Repository.Instance.GetData(); 
     gvAgentList.DataBind(); 
    } 

    protected void gvAgentList_PageIndexChanging (object sender, GridViewPageEventArgs e) 
    { 

     gvAgentList.PageIndex = e.NewPageIndex; 
     gvAgentList.DataBind(); 
    } 

    private string ConvertSortDirectionToSql(SortDirection sortDirection) 
    { 
     string newSortDirection = String.Empty; 

     switch (sortDirection) 
     { 
      case SortDirection.Ascending: 
       newSortDirection = "ASC"; 
       break; 

      case SortDirection.Descending: 
       newSortDirection = "DESC"; 
       break; 
     } 

     return newSortDirection; 
    } 

    protected void gvAgentList_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     DataTable dataTable = gvAgentList.DataSource as DataTable; 

     if (dataTable != null) 
     { 
      DataView dataView = new DataView(dataTable); 
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); 

      gvAgentList.DataSource = dataView; 
      gvAgentList.DataBind(); 
     } 
    } 

} 

這裏是GridView控件的標記:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <p> 
    Agent Lists:</p> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
    <asp:GridView runat ="server" ID = "gvAgentList" 
      AllowPaging = "True" 
      AutoGenerateSelectButton="True" AllowSorting="True" BackColor="#E8E8E8" 
      BorderColor="#003399" BorderStyle="Solid" BorderWidth="1px" Height="375px" 
      Width="731px" OnPageIndexChanging = "gvAgentList_PageIndexChanging" 
      OnSorting="gvAgentList_Sorting" > 

     <AlternatingRowStyle ForeColor="#0066CC" /> 
     <HeaderStyle ForeColor="#3366FF" /> 

    </asp:GridView> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource> 



    </ContentTemplate> 
    </asp:UpdatePanel> 
</asp:Content> 

的問題是,我的GridView控件不扔我任何異常,並通過斷點,我可以看到,該函數被調用在代碼後面。

我的排序也沒有工作:(

+0

難道您發佈的標記代碼爲那麼你需要調用gvAgentList_PageIndexChanging中的BindData方法 – WorldIsRound 2011-03-21 19:24:27

+0

@HelloWorld:我在gvAgentList_PageIndexChanging中調用了DataBind方法 – 2011-03-21 19:31:54

+0

What abou當頁面索引改變時,將數據源設置爲網格? – WorldIsRound 2011-03-21 19:36:24

回答

7

嘗試GridView控件如的PageIndex = 「10」

protected void gvAgentList_PageIndexChanging (object sender, GridViewPageEventArgs e) 
    { 

     gvAgentList.PageIndex = e.NewPageIndex; 
     BindData(); 
    } 
+0

謝謝!我知道我錯過了一些愚蠢的東西。工作! – 2011-03-21 19:51:10

+0

如果我想在我的GridView中進行搜索,應該如何處理? – 2011-03-21 19:53:28