GridView PageIndexChanging 不起作用

发布于 2024-10-24 08:54:28 字数 2791 浏览 4 评论 0原文

这似乎是一个很容易问的问题,但我无法在 GridView 中显示项目。这是我的代码:

 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 没有抛出任何异常,通过断点我可以看到该函数是在后面的代码中调用的。

我的排序也不起作用:(

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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-10-31 08:54:28

尝试

protected void gvAgentList_PageIndexChanging (object sender, GridViewPageEventArgs e)
    {

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

Try

protected void gvAgentList_PageIndexChanging (object sender, GridViewPageEventArgs e)
    {

        gvAgentList.PageIndex = e.NewPageIndex;
        BindData();
    }
灰色世界里的红玫瑰 2024-10-31 08:54:28

设置gridview的pageindex属性,例如PageIndex =“10”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文