• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

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?
0
Psychotext
Asked:
Psychotext
  • 3
  • 3
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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:
Ahh, excellent. Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now