asp .net dropdownlist trim data
In the asp.net dropdownlist i need to trim the data inside the list. e.g if my drop down has 10 record开发者_如何学Cs 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?
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);
}
}
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);
}
}
精彩评论