gpt4 book ai didi

c# - 如何通过在 asp.net 中选择下拉菜单选项来刷新结果顺序

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:59 29 4
gpt4 key购买 nike

我有一个列表:

List<CompanyModel1> companies1 = new List<CompanyModel1>();

其中 CompanyModel1 定义为:

public class CompanyModel1
{
public int compnSN1 { get; set; }
public string compnStyle1 { get; set; }
public string compnId1 { get; set; }
public string compnKeySkills1 { get; set; }
public string compnStandards1 { get; set; }
}

现在,每次用户从下拉列表中单击一个选项时,我都需要对这个列表进行不同的排序。

<asp:DropDownList ID="DropDownList1" runat="server" Width="200px"  
autopostback="true" OnSelectedIndexChanged="DropDownList1sel">
<asp:ListItem Text="abc" Value="0"></asp:ListItem>
<asp:ListItem Text="Iso 9001" Value="1"></asp:ListItem>
<asp:ListItem Text="Iso 2004" Value="2"></asp:ListItem>
<asp:ListItem Text="Standard xyz" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

例如,如果您单击“Iso 2004”,那么它会将所有“compnStandards1”=“Iso 2004”分组,并将其放在最前面,其余的放在后面(重新排列)。

我知道我可以将其分组:

companies1.GroupBy(o => o.compnStandards1).ToList();

但是我如何对前面带有标准值“Iso 2004”的所有结果进行排序?我不能在这里使用正常排序,因为我不是要使用字母排序。此外,如果我需要从不同的 compnStandards1 值填充上述下拉菜单中的选项,是否可能以及如何?

最佳答案

您可以在 DropDownList1 的处理程序中轻松地对列表进行动态排序

protected void DropDownList1sel(object sender, EventArgs e)
{
// sort the companies according the selected standart
var dinamicallySortedList = companies1.
OrderByDescending(c => c.compnStandards1.Equals(DropDownList1.SelectedItem.Text));
// for debug
Label1.Text = string.Empty;
// some work for the showing the companies
foreach (var company in dinamicallySortedList)
{
Label1.Text += company.compnId1 + "<br />";
}
}

在这里,在您选择新的标准后,列表将按照您在公司中每个元素的 compnStandards1 属性中搜索的标准名称的数字索引进行排序。

你可以使用 .Distinct() 方法获取所有标准,如下所示:

foreach (var standart in companies1.Where(c => !string.IsNullOrEmpty(c.compnStandards1)).Select(c => c.compnStandards1).Distinct())
{
DropDownList1.Items.Add(new ListItem(standart));
}

我测试的其余代码:

public partial class _Default : Page
{
List<CompanyModel1> companies1 = new List<CompanyModel1>();

protected void Page_Init(object sender, EventArgs e)
{
companies1.Add(new CompanyModel1
{
compnSN1 = 1,
compnId1 = "Test 1",
compnKeySkills1 = "Key 1",
compnStyle1 = "Style 1",
compnStandards1 = "abc"
});
companies1.Add(new CompanyModel1
{
compnSN1 = 2,
compnId1 = "Test 2",
compnKeySkills1 = "Key 2",
compnStyle1 = "Style 2",
compnStandards1 = "abc"
});
companies1.Add(new CompanyModel1
{
compnSN1 = 3,
compnId1 = "Test 3",
compnKeySkills1 = "Key 3",
compnStyle1 = "Style 3",
compnStandards1 = "Iso 2004"
});
}

public class CompanyModel1
{
public int compnSN1 { get; set; }
public string compnStyle1 { get; set; }
public string compnId1 { get; set; }
public string compnKeySkills1 { get; set; }
public string compnStandards1 { get; set; }
}
}

更新:

我的代码使用 UpdatePanel 中的标签 - 它被用于部分页面更新:

    // sort the companies according the selected standart
var dinamicallySortedList = companies1.
OrderByDescending(c => c.compnStandards1.Equals(DropDownList1.SelectedItem.Text));
// for debug
Label1.Text = string.Empty;
// some work for the showing the companies
foreach (var company in dinamicallySortedList)
{
Label1.Text += company.compnId1 + "<br />";
}

您可以使用 this tutorial 轻松地将 ScriptManager 添加到您的页面,例如。

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" Width="200px"
autopostback="true" OnSelectedIndexChanged="DropDownList1sel">
</asp:DropDownList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

如我所见,代码现在可以运行了,所以您可以随意使用它。但是,这不是您获得问题的完整解决方案的地方。祝这个项目好运。


关于填充结果列表的问题:

After getting companies1, I use your foreach loop to populate the option for dropdown menu.
Then bind companies1 to display the result (it is a search function). The result is display when I bind the data to the repeater at ascx file:

List<CompanyModel1> companies1sorted = companies1.OrderByDescending(o =>o.compnMastery1).ToList();
companyRepeater.DataSource = companies1sorted;
companyRepeater.DataBind();

Then I want if user click options on dropdown menu, it will sort these results.

您必须在每次加载页面时重新填充 .DataSource,因为它没有存储在 ViewState 中,并触发 DataBind 事件每次之后,都是这样的:

protected void DropDownList1sel(object sender, EventArgs e)
{
// sort the companies according the selected standart
var dinamicallySortedList = companies1.
OrderByDescending(c => c.compnStandards1.Equals(DropDownList1.SelectedItem.Text));
companyRepeater.DataSource = dinamicallySortedList;
companyRepeater.DataBind();
}

确保您已在代码中附加到事件:

<asp:DropDownList ID="DropDownList1" runat="server" Width="200px"  
autopostback="true" OnSelectedIndexChanged="DropDownList1sel">
</asp:DropDownList>

对于多项选择,可以使用级联的方法:

companies1.OrderByDescending().ThenByDescending()

关于c# - 如何通过在 asp.net 中选择下拉菜单选项来刷新结果顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24626030/

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