Pages

Dec 18, 2008

How to write a Property for ListBox to Generate ID List for Selected Items + C#

This is Property for ListBox to Generate ID List for Multiple Selected Items.
Use this namespace because here we use regex in the property.
using System.Text.RegularExpressions;

///
/// Gets Or Sets the IDsList.
///

private string IDsList
{
get
{
string IDs = string.Empty;
foreach (ListItem item in lbSample.Items)
{
if (item.Selected)
IDs = IDs + item.Value + ",";
}
IDs = System.Text.RegularExpressions.Regex.Replace(IDs, ",$", "");
return IDs;
}
set
{
foreach (int item in value)
{
if (lbSample.Items.FindByValue(item.ToString()) != null)
lbSample.Items.FindByValue(item.ToString()).Selected = true;
}
}
}

This will automatically return the list of IDs selected in the List box.

No comments: