Displaying ampersand in listbox

I'm trying to get a listbox displaying an ampersand character (&) but I'm running into all sorts of difficulties.   The database behind the system has all ampersands stored as & so that valid xhtml is output to the pages, the problem is that when this data hits the listbox it seems to end up as this in the source:

      &

Anyone know what's going on and how to fix it?
LVL 2
PsychotextAsked:
Who is Participating?
 
mcgantsCommented:
No, my apologies, there isn't an itemdatabound - I've been working on datagrids all day :P
As far as keeping things efficient, the only other way I can think of is to actually replace the items inside the dataset (presumably inside your function pulling from a stored proc) using something like below:
For Each myrow As DataRow In myDataSet.Tables(0).Rows
  replace(myrow(1), "&amp", "&")
Next

Then, in your presentation layer all you need to do is bind the source as above:
lstCategory.DataSource = dsData.Tables(0).DefaultView
lstCategory.DataBind()

Cheers,
mcg
0
 
PsychotextAuthor Commented:
Testing it in plain old HTML:

  <option value="a">Item 1 &amp; 2</option>
  <option value="b">Item 3 & 4</option>
  <option value="c">Item 5 &amp;&nbsp;6</option>

All display properly, but only the first and last are valid.  But this is largely academic as I still need a way to stop .net changing my option text.
0
 
mcgantsCommented:
You could try replacing the value at runtime:  

Private Sub dgSearch_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgSearch.ItemDataBound
    replace(e.Item.Cells(1).text, "&amp", "&")
  End Sub

That should do it
Hope this helps,
mcg
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
mcgantsCommented:
Sorry, i've given you code for binding to a datagrid - i'd still recommend using the 'replace' though, if you get the value before binding it to the listbox then that should still give you the correct character.
0
 
PsychotextAuthor Commented:
That's surreal.  Do we know why / how it automatically replaces the text before it renders?  To test it I had to change from the original:

            lstCategory.DataSource = dsData.Tables(0).DefaultView
            lstCategory.DataBind()

To

            For intLoop = 0 To dsData.Tables(0).Rows.Count - 1
                Dim drpItem As New Web.UI.WebControls.ListItem
                drpItem.Text = Replace(System.Convert.ToString(dsData.Tables(0).Rows(intLoop).Item(1)), "&amp;", "&")
                drpItem.Value = System.Convert.ToString(dsData.Tables(0).Rows(intLoop).Item(0))
                lstCategory.Items.Insert(intLoop, drpItem)
            Next intLoop

To the best of my knowledge there isn't an itemdatabound for a listbox is there?  Is there another way of changing the text without looping through? (I'm not too worried about it as the page doesn't get much use, but I'd like to keep things as efficient as possible)
0
 
PsychotextAuthor Commented:
Ahh, excellent. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.