In the asp.net dropdownlist i need to trim the data inside the list. e.g if my drop down has 10 records in it and i only want to show the first 20 chars of each record, then how do i do it?. Also if records are only 10 chars then from 20 chars the dropdownlist should automatically resize to 10 chars. any ideas?
+1
A:
If you can't trim the data at the source (i.e. the database query or wherever you're getting the data from), then you can just modify the data after the dropdown has been databound.
myDropDown.DataBind();
foreach (var item in myDropDown.Items)
{
if (item.Text.Length > 20)
{
item.Text = item.Text.Substring(0, 10);
}
}
womp
2009-12-16 03:05:31
A:
I can't recall if the ASP.NET version has a Tag property but if it does this would shorten the text and preserve the original value (copied original from womp):
myDropDown.DataBind();
foreach (var item in myDropDown.Items)
{
if (item.Text.Length > 20)
{
item.Tag = item.Text;
item.Text = item.Text.Substring(0, 10);
}
}
If not then maybe Attributes
(forgive me if my syntax is off, no compiler here to verify against):
myDropDown.DataBind();
foreach (var item in myDropDown.Items)
{
if (item.Text.Length > 20)
{
item.Attributes["title"] = item.Text;
item.Text = item.Text.Substring(0, 10);
}
}
Gnoome
2009-12-16 04:05:06