[asp.net]复选框列表状态与 linq

标签: Asp.net Linq C#
发布时间: 2017/3/26 2:42:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

考虑这种情况的下,要检索列表或 IEnumerable checkboxlist 所有未选中的复选框的值。这段代码给我的所有选中的值 (现在) 或 (以前)。怎样才能不受控制的 (现在) 的值和 (以前) 与 linq。谢谢

private IEnumerable <string > selectedValues
    {
        get
        {
    if (ViewState ["selectedValues"] == null  && chapp . SelectedIndex  >=-1 )
    {
        ViewState ["selectedValues"]= chapp .Items.Cast <ListItem >()
            .Where (li=>li.Selected )
            .Select (li=>li.Value )
            .ToList ();
    }else 
        ViewState ["selectedValues"]= Enumerable .Empty <string >();
            return (IEnumerable <string >)ViewState ["selectedValues"];
        }
        set {ViewState ["selectedValues"] = value ;}
        }
protected void chapp_SelectedIndexChanged(Object sender, EventArgs e)
 {
     CheckBoxList c = (CheckBoxList)sender;

   var oldselection = this.selectedValues;
     var newSelection = c.Items.Cast<ListItem>()
                 .Where(li => li.Selected  )
                 .Select(li => li.Value);
     var unchangedItems = newSelection.Except(oldselection );

 }

解决方法 1:

基于你评论︰ 比方说,你有 aspx 页面的加载你有 (从 active directory,DB 或无论) 之后,分配的所有应用程序的所有应用程序的当前用户具有 (也从究竟得到这些应用程序)...所以在你 Page_Load 你会得到类似下面︰

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Load all applications
        var apps = loadAllApplications();
        //Load user applications
        var myApps = loadUserApplications();

        //Bind to checkboxlist, assuming my checkboxlist ID "chksApps"
        chksApps.DataSource = apps.Select(x => new ListItem
        {
            Value = x.Id.ToString(),
            Text = x.Name,
            Selected = myApps.Any(a => x.Id == a)
        });
        chksApps.DataBind();
    }
}

//lets say I have a Application class like that
public class Application
{
    public int Id { get; set; }
    public string Name { get; set; }
}

private List<int> loadUserApplications()
{
    // if user already have "Paint", "Chrome" assigned
    var myApps = new List<int>() { 2, 4 };
    return myApps;
}

private List<Application> loadAllApplications()
{
    //for testing I will create a dummy list of applications
    var applications = new List<Application>() { 
        new Application { Id = 1, Name = "Visual Studio" },
        new Application { Id = 2, Name = "Paint" },
        new Application { Id = 3, Name = "Notepad" },
        new Application { Id = 4, Name = "Chrome" }
    };
    return applications;
}

所以直到现在的用户将能够检查,取消选中他什么都想要。所以你如何AutoPostBack = False

所以,当他完成编辑,假设他将向服务器提交的这样他会有一个提交按钮 (让我们说它的 ID 是"btnSave"

 protected void btnSave_Click(object sender, EventArgs e)
 {
     //Load user applications
     var myApps = loadUserApplications();

     //get selected Application
     var selectedApps = chksApps.Items.Cast<ListItem>()
                            .Where(x => x.Selected)
                            .Select(x => int.Parse(x.Value)).ToList();

     // send request to owner to add those below apps for user "newSelectedApps"
     var newSelectedApps = selectedApps.Except(myApps).ToList();

     // send request to owner to remove those below apps "newUnSelectedApps"
     var newUnSelectedApps = myApps.Except(selectedApps).ToList();

     // those below are the unchanged apps "unChangedApps"
     var unChangedApps = myApps.Intersect(selectedApps).ToList();
 }

这是比使用 ViewState 的性能

赞助商