English 中文(简体)
How to force a line break in the listitems of a dropdownlist?
原标题:

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 " " 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 " " 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?

最佳回答

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:

<select>
    <option value="A">A</option>
    <option value="A">A.2</option>
    <option value="B">B</option>
</select>

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.

问题回答

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."





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签