简体   繁体   English

ASP.NET烦人的URL编码字符串

[英]ASP.NET annoying URL encoding strings

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: 然后继续创建以下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; 这是url编码&nbsp; to &amp;nbsp; &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. 我需要选项中的初步空格。

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) 编辑:是具有UTF-8 U + 00A0值的字符(例如,它呈现为空格,但不被任何IsSpace方法变体视为空格)

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. 据我所知,你必须迭代DropDownList的Items集合,并在每个选项的Text属性上调用HttpUtility.HttpDecode。 I don't believe there's a way to prevent HTTP encoding for text values. 我不相信有办法阻止文本值的HTTP编码。

As an aside, do you know about the OPTGROUP tag? 顺便说一句,你知道OPTGROUP标签吗? Unfortunately it cannot be nested (yet) but it may cover some of your cases: 不幸的是它不能嵌套(但)它可能涵盖你的一些情况:

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

-Oisin -Oisin

我会用css格式化间距...基本上你不需要文本,你只想在视觉上格式化选项...

The text is being double-HTMLEncoded for some reason. 由于某种原因,该文本被双重HTMLEncoded。

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM