【问题标题】:Sorting using a Repeater with pagination使用带有分页的Repeater进行排序
【发布时间】:2015-06-26 08:57:21
【问题描述】:

我有一个中继器,我正在使用分页。它有效,但它对我的排序做了一些有趣的事情。首先,如果我按下排序按钮,我的分页控件会显示两次。其次,它根据默认排序顺序进行分页。有什么想法可能是错的吗?

    protected void btnSort_Click(object sender, EventArgs e)
    {
        Show_Data();
    }

    public void Show_Data()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString);
        string srtOrder = cboSortBy.Text;
        SqlDataAdapter adp = new SqlDataAdapter("select [ACCT_LIST].*, [ACCT_GRP_LIST].ACCT_GRP from [ACCT_LIST] LEFT JOIN [ACCT_GRP_LIST] on [ACCT_GRP_LIST].ACCT_GRP_PK = [ACCT_LIST].ACCT_GRP_FK ORDER BY " + srtOrder + "", con);
        DataSet ds = new DataSet();
        adp.Fill(ds, "TAcctList");

        //Pagination code so only a set number of records loads at a time.
        //  Done to speed up the loading, since this list gets really long.
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables["TAcctList"].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 20;

        int currentPage;

        if (Request.QueryString["page"] != null)
        {
            currentPage = Int32.Parse(Request.QueryString["page"]);
        }
        else
        {
            currentPage = 1;
        }

        pds.CurrentPageIndex = currentPage - 1;
        Label1.Text = "Page " + currentPage + " of " + pds.PageCount;

        if (!pds.IsFirstPage)
        {
            MenuItem itemMessage = NavMenu.FindItem("First");
            itemMessage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
        }

        AcctRepeater.DataSource = pds;
        AcctRepeater.DataBind();

        CreatePagingControl(pds.PageCount, pds.CurrentPageIndex);
        // End of Pagination code

        con.Close();
    }

在 ASP.Net 端,按钮控件如下所示:

<table>
<tr>
    <td width="150"><asp:DropDownList ID="cboSortBy" runat="server" Width="120">
                                <asp:ListItem Value="StatusText">Benefit Type</asp:ListItem>
                                <asp:ListItem Value="PRIORITY_RANK">Priority Rank</asp:ListItem>
                                <asp:ListItem Value="ACTIVE_FLG">Active Flag</asp:ListItem>
                                </asp:DropDownList></td>
    <td width="180"><asp:Button ID="btnSort" runat="server" 
        Text="Sort" Width="121px" onclick="btnSort_Click" /></td>
</tr>
</table>

分页是新的,但在添加之前,排序功能运行良好。现在分页部分工作正常,但排序部分变得不稳定。我不知道分页的哪一部分把它搞砸了。

【问题讨论】:

  • 我认为控件显示两次是因为我没有进行回发?不过,不确定排序问题。
  • 好的,我认为部分问题是表单正在回发,并且包含排序值的组合框被重置为默认值。我的分页创建的链接如下所示:localhost:50361/AdminForms/frmManageBenefitType.aspx?page=3。所以,我猜当您单击它会执行完整的回发,这会重置排序组合。有人知道如何对抗吗?我想我需要以某种方式将组合选择的值向前推进。

标签: c# asp.net pagination repeater code-behind


【解决方案1】:

试试

ds.Tables["TAcctList"].DefaultView.Sort = "PRIORITY_RANK ASC";

【讨论】:

  • 重点是,它又回到了默认值。硬编码排序顺序对我没有帮助,我需要让它在刷新时识别排序顺序。
  • 不要硬编码。从您的下拉列表中获取它。我不认为我必须得到那个具体。对不起。重点是,按照上面的方式设置。
猜你喜欢
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多