tags:

views:

46

answers:

1
ddlCats.DataSource = listOfCats;
ddlCats.DataBind();

If an item contains say, &, it will appear in the ddl as &

How can I stop this? I am using asp.net

A: 

im having the same problem here and here is the code for those interested (not a fix but a example of the problem )

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="HtmlEncodeDropDownList.aspx.cs" Inherits="ForumExampleCode_HtmlEncodeDropDownList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head runat="server">
        <title>HTML Encode Drop Down List Example (english, c# only)</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <h1>HTML Encode Drop Down List Example (english, c# only)</h1>
                <asp:dropdownlist id="ddlCountry" runat="server" />
                <div>
                    This is how it should look in the drop down list:<br />
                    c&#244;te d&#39;ivoire<br />
                    s&#227;o tom&#233; and pr&#237;ncipe
                </div>
            </div>
        </form>
    </body>
</html>

and code behind is here

using System;

using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

public partial class ForumExampleCode_HtmlEncodeDropDownList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { #region region 1: create a datatable and fill with values

    //note: declare objects
    DataTable countryTable = new DataTable();

    //note: add data columns to datatabl
    DataColumn newColumn1 = new DataColumn();
    newColumn1.AllowDBNull = false;
    newColumn1.Caption = "three letter ISO country abbreviation";
    newColumn1.ColumnName = "iso";
    newColumn1.DataType = System.Type.GetType("System.String");
    countryTable.Columns.Add(newColumn1);

    //note: add data columns to datatable
    DataColumn newColumn2 = new DataColumn();
    newColumn2.AllowDBNull = false;
    newColumn2.Caption = "country name";
    newColumn2.ColumnName = "countryName";
    newColumn2.DataType = System.Type.GetType("System.String");
    countryTable.Columns.Add(newColumn2);

    //note: rename table
    countryTable.TableName = "CountryList";

    //note: add row
    DataRow newRow1 = countryTable.NewRow();
    newRow1["iso"] = "abc";
    newRow1["countryName"] = "c&#244;te d&#39;ivoire";
    countryTable.Rows.Add(newRow1);

    //note: add row
    DataRow newRow2 = countryTable.NewRow();
    newRow2 = countryTable.NewRow();
    newRow2["iso"] = "xyz";
    newRow2["countryName"] = "s&#227;o tom&#233; and pr&#237;ncipe";
    countryTable.Rows.Add(newRow2);

    #endregion

    #region region 2: bind datatable to drop down box

    ddlCountry.DataSource = countryTable;
    ddlCountry.DataTextField = "countryName";
    ddlCountry.DataValueField = "iso";
    ddlCountry.DataBind();

    #endregion
}

}

wfpkhc
it's display exactly what you tell it to. if you want it displayed how you intend, you fill your table with `newRow1["countryName"] = "côte d'ivoire";`. the control handles the encoding for you.
lincolnk
theory question: what if I dont have control over the datatable content being provided? what if it is being provided to me as a datatable? how do I stop it from htmlencoding it?
wfpkhc
@wfpkhc i dunno offhand. ask a new question and i bet someone will show you how.
lincolnk
wfpkhc
@wfpkhc SO operates on a system of ask one question, get specific answers. it's atypical to carry on a discussion like this around here. your original post should have been it's own question since it doesn't solve the original poster's problem. also your problem seems be different which doesn't necessarily do any good for the OP nor is it going to garner the appropriate attention to fix your issue.
lincolnk
sorry im new to this website.... but my post was a way to demonstrate to any reader how to recreate the original posters problem and thus someone might be able to provide a solution.
wfpkhc