4

I have this small function:

// Adds to menu
public void addMenuToList(int menuVal, string menuTxt, int depth, bool hasChildren)
{
    for (int i = 0; i < depth; i++)
    {
        menuTxt = "&nbsp;" + menuTxt;
    }
    if (hasChildren) { menuTxt = " + " + menuTxt; }

    ListItem newItem = new ListItem();
    newItem.Text = menuTxt;
    newItem.Value = menuVal.ToString();
    parent.Items.Add(newItem);
}

Which then goes on to create the following HTML:

<select size="4" name="ctl00$mainContent$parent" id="ctl00_mainContent_parent" class="tbox widebox">
    <option selected="selected" value="0">Top Level</option>
    <option value="1"> + Boxes</option>
    <option value="2">&amp;nbsp;&amp;nbsp;Wrapping</option>    
    <option value="8"> + &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;All Products</option>
</select>

It's url encoding the &nbsp; to &amp;nbsp; which spoils the formatting of the rendered select box. Any ideas how to prevent this happening? I need preliminary spaces in the options.

2
  • What happens if you just use " " instead of "&nbsp;"? Commented Aug 24, 2010 at 13:52
  • The empty char doesn't render as preliminary characters in select boxes. HTML seems to only allow one, any more and you have to use &nbsp; Commented Aug 24, 2010 at 13:53

4 Answers 4

7

Try following:

string space = Server.HtmlDecode("&nbsp;");

for (int i = 0; i < depth; i++)
{
    menuTxt = space + menuTxt;
}

EDIT: nbsp is character with UTF-8 U+00A0 value (eg. it renders as a space, but isn't considered as a space by any of IsSpace method variants)

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I know you'll have to iterate the DropDownList's Items collection and call HttpUtility.HttpDecode on each option's Text property. I don't believe there's a way to prevent HTTP encoding for text values.

As an aside, do you know about the OPTGROUP tag? Unfortunately it cannot be nested (yet) but it may cover some of your cases:

http://htmlhelp.com/reference/html40/forms/optgroup.html

-Oisin

1 Comment

Thank you, excellent answer which was correct and reminded me of an html element I don't use often which would be better. I only accepted the other answer because he was first :)
0

I would format the spacing with css ... essentially you don't need text, you just want to format the options visually ...

1 Comment

I don't think it's possible to format individual list options? Or maybe it is. Either way it wouldn't be scalable for something of n depth.
0

The text is being double-HTMLEncoded for some reason.

Try this:

// Adds to menu
    public void addMenuToList(int menuVal, string menuTxt, int depth, bool hasChildren)
    {
        for (int i = 0; i < depth; i++)
        {
            menuTxt = "&nbsp;" + menuTxt;
        }
        if (hasChildren) { menuTxt = " + " + menuTxt; }

        ListItem newItem = new ListItem(menuVal.ToString(), menuTxt);
        parent.Items.Add(newItem);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.