LINQ to DropDownList with Server.HtmlDecode

时间:2011-02-18 04:12:30

标签: c# linq drop-down-menu html-encode

我有这个代码使用LINQ来填充DropDownList:

var userCategories = DataAccessLayer.Context.Categories
    .Where(c => c.UserName == HttpContext.Current.User.Identity.Name)
    .Select(c => new { c.ID, c.Category })
    .OrderBy(c => c.Category);

CategoryDropDownList.DataSource = userCategories;

CategoryDropDownList.DataValueField = "ID";

CategoryDropDownList.DataTextField = "Category";

CategoryDropDownList.DataBind();

我将Server.HtmlDecode放在哪个类别?

1 个答案:

答案 0 :(得分:1)

首先,你需要HtmlEncode,而不是HtmlDecode。这是一个例子:

foreach (var category in userCategories)
{
    CategoryDropDownList.Items.Add(new ListItem(Server.HtmlEncode(category.Category), category.ID.ToString()));
}