tags:

views:

120

answers:

4

I have the following code:

Dim db As New linqclassesDataContext
Dim categories = (From c In db.faq_cats)
NewFaqDropDownCategory.DataSource = categories
NewFaqDropDownCategory.DataTextField = "category"
NewFaqDropDownCategory.DataValueField = "category_id"
If Not Page.IsPostBack Then
    NewFaqDropDownCategory.DataBind()
End If
Unset(categories)
Unset(db)

One of the items under the column "category" has & in the title, and that shows up in the dropdownlist as such. Is there someway to make the list display "&" instead?

One Solution

I figured I could use the .Replace() function to do this, and I accidentally found out how:

For Each c In categories
    If c.category.Contains("&") Then
        c.category = c.category.Replace("&", "&")
    End If  
Next

I could expand this in the future to process other values as well.

A: 

I suggest you do .ToList(), and then have a Select that grabs it doing .Replace()

The reason to do ToList, is to get it as on memory, from then on you get to are no longer constrained to constructs that exists on sql. I don't know the vb.net syntax, but in c# the select would look like:

new {
     category = c.category.Replace("&", "&"),
     c.category_id
}
eglasius
A: 

you could do this in your bound item

<%# DataBinder.Eval(Container.DataItem, "Category").ToString().Replace("&amp;", "&") %>

or in your code behind OnItemDataBound

hunter
+1  A: 

If there are other HTML encoded characters in there as well you could use HttpUtility.HtmlDecode(c.category). This would prevent your replace and ensure that any characters are properly decoded.

I don't know if this is exact VB linq anonymous object syntax, but I tried.

Dim datasource = From categories In db.faq_cats _
                 Select New With { .category = HttpUtility.HtmlDecode(categories.category), .category_id = categores.category_id }

Then bind your DDL to that datasource.

You could also just use .Replace if you only need &amp;

Dim datasource = From categories In db.faq_cats _
                 Select New With { .category = categories.category.Replace("&amp;", "&"), .category_id = categores.category_id }
Quintin Robinson
+1  A: 

One of the items under the column "category" has &amp; in the title

If that &amp; is really supposed to be a ‘&’, I suggest your database needs cleaning to make it one. Generally you always want plain text in your database; the HTML-escaping shouldn't happen until the final output-to-page stage.

Storing data in different stages of encoding is confusing and potentially dangerous to work with; get the wrong level of encoding in the other direction and you have cross-site-scripting attacks.

bobince
thank you for the pointer, i will be revising my code.
Anders