简体   繁体   中英

Using < > in text for asp:DropDownList option

I have the following in my .aspx file:

    <asp:DropDownList ID="title" Width="350px" runat="server">
        <asp:ListItem Value="0">Please Select</asp:ListItem>
        <asp:ListItem Value="Dr">Dr</asp:ListItem>
        <asp:ListItem Value="Mr">Mr</asp:ListItem>
        <asp:ListItem Value="Ms">Ms</asp:ListItem>
        <asp:ListItem Value="Miss">Miss</asp:ListItem>
        <asp:ListItem Value="Mrs">Mrs</asp:ListItem>
    </asp:DropDownList>

I would like to put < > around "Please Select":

<asp:ListItem Value="0"><Please Select></asp:ListItem>

However, the file thinks I am opening a new tag when I use them.

Any ideas on how I can use these, possibly escaping them etc.,?

There's a Text attribute you can use for the text instead of putting the text between the opening and closing tags:

<asp:ListItem Value="0" Text="<Please Select>" />

You can also use the HTML entities if you prefer:

<asp:ListItem Value="0" Text="&lt;Please Select&gt;" />

You can use HttpServerUtility.HtmlEncode function

String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);

msdn link : http://msdn.microsoft.com/fr-fr/library/vstudio/w3te6wfz.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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