Vinit Patel

Get First and last values from CheckBoxList

Posted on: January 6, 2012


If you have CheckboxList like

0 – 5

5 – 10

10 – 15

15 – 20

25+

If you had selected multiple like 5 – 10 & 10 – 15 then Values be like 5 – 15.

So, Here is the code for that,

protected void btn1_Click(object sender, EventArgs e)
{
String values = “”;
for (int i = 0; i < cbl1.Items.Count; i++)
{
if (cbl1.Items[i].Selected)
{
values += cbl1.Items[i].Value + “,”;
}
}
values = values.TrimEnd(‘,’);
lbl1.Text = Combine(values);
}

 

//Function which actually returns the combine values

public string Combine(string input)
{
//First remove all the white spaces
string result = input.Replace(” “, string.Empty);
//Split on each ,
string[] results = result.Split(‘,’);
//Now split on each –
List<string> final = new List<string>();
//Split on each dash
foreach (string s in results)
final.AddRange(s.Split(‘-‘));
//Remove duplicates
final = final.Distinct().ToList();
//Return the first and the last element
return string.Format(“{0} – {1}”, final.First(), final.Last());
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Calendar

January 2012
M T W T F S S
« Jun    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Stats Of Blog

  • 6,792 hits

Cluster Map

%d bloggers like this: