?
Solved

Prevent HTML entity encoding when binding to a dropdown list

Posted on 2005-05-11
19
Medium Priority
?
975 Views
Last Modified: 2008-01-09
Im looking for a solution to this problem.  

My input data has the   in it.  When I bind this data to a dropdownlist control the & gets encoded to &   Is there a way to prevent my data from being encoded on the bind?


= Input Data =

1 |   One
2 |   Two
3 |   Three

= HTML Output =

<option value="1">&amp;nbsp;&amp;nbsp;One</option>
<option value="2">&amp;nbsp;&amp;nbsp;Two</option>
<option value="3">&amp;nbsp;&amp;nbsp;Three</option>

= Needs to be =

<option value="1">&nbsp;&nbsp;One</option>
<option value="2">&nbsp;&nbsp;Two</option>
<option value="3">&nbsp;&nbsp;Three</option>
0
Comment
Question by:g9support
  • 12
  • 6
19 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 13982160
I say "no", not easily anyway.  I check the source of both the dropdownlist and htmlselect and they have HttpUtility.HTMLencode(value) when it is rendering the tag.  You however can create your own custom control with custom rendering options, eg.

Public Class MyDropDownList : Inherits System.Web.UI.Webcontrols.DropDownList

Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
      Dim collection1 As ListItemCollection = Me.Items
      Dim num1 As Integer = Me.Items.Count
      Dim flag1 As Boolean = False
      If (num1 > 0) Then
            Dim num2 As Integer
            For num2 = 0 To num1 - 1
                  Dim item1 As ListItem = collection1.Item(num2)
                  writer.WriteBeginTag("option")
                  If item1.Selected Then
                        If flag1 Then
                              Throw New HttpException(HttpRuntime.FormatResourceString("Cant_Multiselect_In_DropDownList"))
                        End If
                        flag1 = True
                        writer.WriteAttribute("selected", "selected", False)
                  End If
                  writer.WriteAttribute("value", item1.Value, True)
                  writer.Write(">"c)

                  'HttpUtility.HtmlEncode(item1.Text, writer)  *****This line was your problem******
                  writer.Write(item1.Text)                           'Fixed!

                  writer.WriteEndTag("option")
                  writer.WriteLine
            Next num2
      End If
End Sub

End Class

Now just use this like you'd use any other custom control.
--Michael
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 2000 total points
ID: 13985823
There are two ways to do this:

VB.NET
====
Dim padding As String = Chr(160) & Chr(160)
ddl.Items.Add(New ListItem(padding & "Hello World","123"))

C#
===
string padding = "\xA0\xA0";
ddlItems.Add(new ListItem(padding + "Hello World","123"));

<option value="123">&#160;&#160;Hello World</option>

Or set it in the data source

VB.NET
====
Dim table As New DataTable
table.Columns.Add("Value", GetType(String))
table.Columns.Add("Text", GetType(String))
Dim padding As String = Chr(160) & Chr(160)
table.Rows.Add(New Object() {padding & "123", "123"})

ddl.DataValueField = "Field"
ddl.DataTextField = "Text"
ddl.DataSource = table
ddl.DataBind()

C#
===
DataTable table = new DataTable();
table.Columns.Add("Value", typeof(string));
table.Columns.Add("Text", typeof(string));
string padding = "\xA0\xA0";
table.Rows.Add(new object [] {padding + "123","123"});
ddl.DataValueField = "Field";
ddl.DataTextField = "Text";
ddl.DataSource = table;
ddl.DataBind();



0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13985831
The char 160 is the non-breaking space entity &nbsp; = &#160;
0
Independent Software Vendors: 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!

 
LVL 23

Expert Comment

by:b1xml2
ID: 13985853
In MS SQL Server, you can even pass this in your stored procedures (like so):

SELECT
      id,
      Char(160) + Char(160) + Char(160) + [name]
FROM
      tbl_Users
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13985860
so you really do not need to do anything special aside from changing the data.
0
 
LVL 33

Expert Comment

by:raterus
ID: 13985985
well isn't that neat :-)

b1xm2, how would you answer his original question, say he absolutely needed &nbsp; in there?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986016
1. the &nbsp; is the same as &#160;
2. What he wants is the padding inside the DropDownList.
3. This really is quite a common requests regarding padding inside the DropDownList, ListBox (all controls deriving from ListControl)

I would say that I have answered his question.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986035
because the only purpose of the nbsp entity is to provide the padding in this specific case. Surely, even you can recognise and acknowledge this.!
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986126
You see oftentimes even though they ask the question as framed above, all they are saying here is whether they can have padding inside the ListControl. Adding empty spaces will not do it as the browser rendering engine collapses the spaces. It makes senses therefore that they attempt to place nbsp entities in the dropdownlist hoping that the padding shows.

If they were aware of what other ways to provide for padding in the dropdownlists, they would have said so. It's simply because they do not have the knowledge of how to get around this problem,that they ask the question as above.

In my opnion, it is painful to extend each and every ListControl to allow for such simple padding. Better to recognise the way in which padding and other characters could be inserted...
0
 
LVL 33

Expert Comment

by:raterus
ID: 13986168
That's the first time I'd ever seen anyone offer that suggestion, so thanks, you taught me something :-)  I was asking my question hypothetically, for the oddball reason that they had to have &nbsp; or some other special character in there, that HTMLencode would ruin for them.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986225
when I was looking for the solution to this same problem (how to pad my ListItems in 2002), I knew that in XML, &#160; was the same as &nbsp; and was to be used instead of &nbsp; So i tried that out and I was pretty pleased...
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986255
The only thing I will gripe about the ListItemCollection is that any attributes I pass to my ListItems will not be honoured. Like for example a forecolor of red for retired items in the list.
0
 
LVL 33

Expert Comment

by:raterus
ID: 13986262
http:Q_21421569.html  I fixed your little oops b1xml2 :-)
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986276
lol thanks raterus !!!
0
 
LVL 33

Expert Comment

by:raterus
ID: 13986281
Yeah, I avoid the radiobuttonlist/checkboxlist, where you might actually have to add additionaly attributes, I got burned once by the fact you couldn't add something as simple as an onclick javascript event.  the HtmlSelect will allow you to add additional attributes though.
0
 
LVL 33

Expert Comment

by:raterus
ID: 13986296
I was reading that question and thought, "Gosh!, what are the chances of being able to cut/paste the same response in the same day!"
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986334
I thought that question was this, and was horrified when it wasnt !
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13986346
Now the only consideration for the asker here is how difficult is it for him/her to change &nbsp; to &#160; in the data
0
 

Author Comment

by:g9support
ID: 13987688
As it turns out, we only need padding for the dropdown list.  I wasn't aware or the &#160; and assumed that the &nbsp; was the only way to do it.  The &#160; works just fine and puts spaces where they need to be.

That you all for your help.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question