如何在下拉列表的列表项中强制换行?
我有一个下拉列表,我已经手动进行数据绑定。我当前获取数据,循环遍历每个项目,并根据项目中的值更改单个 ListItem 的 css。
例如:
foreach (Site mySite in myList)
{
ListItem li = new ListItem(mySite.FullAddress, mySite.Id.ToString());
if (mySite.DisabledFlg)
{
li.Attributes.Add("style", "color:#999");
}
this.Items.Add(li);
}
我试图强制列表项中的文本在某些点处中断。我从数据源检索的字符串当前在应该换行的地方都有一个“\r\n”。这对于在普通文本框中显示效果很好,但在列表项上不起作用。 我尝试将“\r\n”替换为 “
但当我这样做时,它只在列表中显示
”
以及文本的其余部分。我尝试将列表项内容包装在带有宽度属性的 "div"
中,但它也只是在 ListItem 文本中按字面显示 "div"
标记。 我还尝试将以下内容添加到绑定时的 ListItem 属性中:
li.Attributes.Add("width", "100px");
但这也没有导致任何更改。
我见过其中包含多行列表项的自定义下拉列表控件。我怎样才能达到相同的结果?
I have a dropdownlist that I already have manually databinding. I currently get the data, loop through each item, and depending on values in the item, change the css for the individual ListItem.
For example:
foreach (Site mySite in myList)
{
ListItem li = new ListItem(mySite.FullAddress, mySite.Id.ToString());
if (mySite.DisabledFlg)
{
li.Attributes.Add("style", "color:#999");
}
this.Items.Add(li);
}
I'm trying to force the text in the listitem to break at certain points. The string that I retrieve from the datasource currently has a "\r\n" everywhere where there should be a line break. This works fine for display in a normal textbox, but it doesn't work on the listitem.
I've tried replacing the "\r\n" with a "<br>"
but when I do that, it just displays <br>
in the listem along with the rest of the text. I've tried wrapping the listitem contents in a "div"
with a width attribute, but it also just display the "div"
tag literally in the ListItem text.
I've also tried adding the following to the ListItem attributes at bind:
li.Attributes.Add("width", "100px");
But that hasn't resulted in any change either.
I've seen custom drop down list controls that have multiline listitems in them. How do I acheive the same result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是不可能的。 DropDownList 控件呈现一个
然而,实现相同效果的另一种方法是在将数据绑定到 DropDownList 之前处理数据。您可以拆分换行符,然后将相同的 DataValue 分配给那些重复的项目。您最终会得到如下所示的 HTML:
您也许还可以找到一些奇特的 Javascript 控件或插件,它们可以模拟下拉控件,但比普通的
select
元素具有更大的灵活性。I don't think this is possible. The DropDownList control renders a
<select>
element and it's just not designed for its child<option>
elements to have line breaks.Another way to achieve the same effect however would be to process your data before binding it to the DropDownList. You could split on the line feeds and just assign the same DataValue to those duplicated items. You would end up with HTML like the following:
You might also be able to find some fancy Javascript controls or plug-ins that would simulate a dropdown control but allow you more flexibility than the plain
select
element.我认为您必须添加换行符作为它自己的列表项。然后,您可以执行一些 JavaScript 和/或验证来防止用户选择该“项目”。
I think you have to add the line break as it's own list item. You can then do some Javascript and/or validation to prevent the user from selecting that "item."