开发者

WebForms Passing CheckboxList Values To List<T>

I have a GridView which I have a 开发者_C百科List bound to - I want to be able to allow filter of the data based on multiple CheckBoxLists.

For arguments sake let's say I have a List of Jobs, and I want to filter the jobs by code - So I would have a CheckBoxList like

  • C#
  • ASP.NET
  • php
  • F#
  • etc..

If someone selects multiple codes, how do you pass the values into a List to rebind to the GridView? Is there a way to pass values as an array? Or maybe a comma seperated string?

Any examples (I'm a C# boy) would be greatly appreciated, and I hope I have explained it properly :S


use an ObservableCollection<T> . it automatically allows the gridview to "observe" that the underlying datasource has changed and thus update itself.


wherever you do your filtering for the gridview you have to build the list manually before you filter.

var languages = new List<string>();
foreach (ListItem item in cblLanguages.Items)
{
    if (item.Selected)
    {
        languages.Add(item.Value);
    }
}

then when you filter you can do something like (example using linq2sql)

var jobs = db.Jobs.Where(x => langauges.Contains(x.LanguageCode));
gvJobs.DataSource = jobs;
gvJobs.DataBind();


I'm not sure I completely understand your question. But I often do the following to get ListItems into a form queryable via LINQ to objects:

var items = cblLanguages.Items.Cast<ListItem>();

// Selected Items
var selectedItems = items.Where(li => li.Selected);

// Item's containing 'C'
var itemsWithC = items.Where(li => li.Text.Contains("C"));

// Values between 2 and 5
var itemsBetween2And5 = from li in items
                        let v = Convert.ToInt32(li.Value)
                        where 2 <= v && v <= 5
                        select li;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜