ddlCats.DataSource = listOfCats;
ddlCats.DataBind();
如果某个项目包含say,&,它将在ddl中显示为&
我怎么能阻止这个?我正在使用asp.net
答案 0 :(得分:0)
我在这里有同样的问题,这里是感兴趣的人的代码(不是修复,而是问题的一个例子)
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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ôte d'ivoire<br />
são tomé and príncipe
</div>
</div>
</form>
</body>
</html>
后面的代码就在这里
using System;
使用System.Collections.Generic; 使用System.Data; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControls;
public partial class ForumExampleCode_HtmlEncodeDropDownList:System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { #region region 1:创建数据表并填充值
//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ôte d'ivoire";
countryTable.Rows.Add(newRow1);
//note: add row
DataRow newRow2 = countryTable.NewRow();
newRow2 = countryTable.NewRow();
newRow2["iso"] = "xyz";
newRow2["countryName"] = "são tomé and prí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
}
}