gpt4 book ai didi

c# - ListView 排序未设置 SortExpression 和 SortDirection

转载 作者:行者123 更新时间:2023-11-30 12:31:47 25 4
gpt4 key购买 nike

我正在处理一个 ListView 并且感觉在实现排序时我做的事情根本上是错误的。我无法依赖于 List1.SortExpressionList1.SortDirection 的值,而是求助于隐藏字段,因为 List1.SortExpression始终为空白,List1.SortDirection 始终为 SortDirection.Ascending

在我的 .aspx 页面上:(删除了不相关的代码)

<asp:HiddenField runat="server" ID="hdnSortExpression" />
<asp:HiddenField runat="server" ID="hdnSortDirection" />

<asp:ListView runat="server" ID="List1"
OnItemCommand="List1_ItemCommand"
OnSorting="List1_Sorting">
<LayoutTemplate>
<table border="0" cellpadding="1">
<thead>
<tr>
<th><asp:LinkButton runat="server" ID="BtnCompanyCode" CommandName="Sort" CommandArgument="CompanyCode" Text="Company Code" /></th>
... more columns ...
</tr>
</thead>

<tbody>
<tr runat="server" id="itemPlaceholder"></tr>
</tbody>
</table>
</LayoutTemplate>

在我的代码背后:

protected void List1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// empty for now
}

protected void List1_Sorting(object sender, ListViewSortEventArgs e)
{
SortDirection sortDirection;
String sortExpression = e.SortExpression; // how we need to do it in Sorting

if (hdnSortExpression.Value.ToLower() == sortExpression.ToLower())
sortDirection = hdnSortDirection.Value == SortDirection.Ascending.ToString() ? SortDirection.Descending : SortDirection.Ascending;
else
sortDirection = SortDirection.Ascending;

DoSortList(sortExpression, sortDirection); // this sets column headings' sort indicator arrows
List1_BindData(sortExpression, sortDirection); // sets DataSource and calls DataBind()

// the hacky part: setting hidden fields to store sortExpression and sortDirection
hdnEligibilitySortExpression.Value = sortExpression;
hdnEligibilitySortDirection.Value = sortDirection.ToString();
}

private void List1_BindData(String sortExpression, SortDirection sortDirection)
{
List<SomeEntity> list = null;

list = SomeEntity.GetBySsn(_ssn).ToList(); // methods generated by an ORM pointing to an Oracle database

switch (sortExpression.ToLower())
{
case "CompanyCode":
if (sortDirection == SortDirection.Ascending)
List1.DataSource = list.OrderBy(o => o.CompanyCode);
else
List1.DataSource = list.OrderByDescending(o => o.CompanyCode);
break;

... other cases ...
}

List1.DataBind();
}

它有效——每一列都正确排序,单击同一列会反转排序方向,但我不得不得出结论,我错误地连接了东西,因为我不能依赖 SortDirection排序表达式 属性。我做错了什么?

最佳答案

看看这个MSDN example .您会注意到 DataSource 是一个 SqlDataSource。这是 .Net 理解如何排序的数据源类型之一。其他的,没那么多。

这是另一个 MSDN article指定数据源类型:

The data source controls that provide built-in support for sorting are the LinqDataSource, ObjectDataSource, SqlDataSource, and AccessDataSource controls.

关于c# - ListView 排序未设置 SortExpression 和 SortDirection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com