2011-02-18 27 views
0

DROPDOWNLIST我有這樣的代碼,它使用LINQ填充一個DropDownList:LINQ與Server.HtmlDecode

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的範疇?

+0

是什麼讓你覺得你需要? – 2011-02-18 04:17:11

回答

1

首先,你想要HtmlEncode,而不是HtmlDecode。這裏有一個例子:

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

你的代碼工作得很好,謝謝你。然而,我確實需要做HtmlDecode,因爲我將它編碼到數據庫中,現在我正在解碼它以向用戶顯示DropDownList中的項目。謝謝! – user390480 2011-02-19 01:01:53