GridView PageIndexChanging Not Working

断了今生、忘了曾经 提交于 2019-12-11 04:18:01

问题


This seems to be an easy question to ask, but I am not able to show items in GridView. Here is my code:

 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();
       }
   }

}

Here is the markup of the 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>

The problem is that my GridView is not throwing me any exception and through breakpoints I can see that the function is called in code behind.

My sorting is also not working :(


回答1:


Try

protected void gvAgentList_PageIndexChanging (object sender, GridViewPageEventArgs e)
    {

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



回答2:


Set pageindex property of gridview e.g. PageIndex="10"



来源:https://stackoverflow.com/questions/5382477/gridview-pageindexchanging-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!