DropDown List eliminate intermediate spaces

  • Thread starter Thread starter Lucas Campos
  • Start date Start date
L

Lucas Campos

Hello,

I'm using a dropdownlist web control. This dropdownlist has a problem with
some descriptions I'd like to show.

The description is "XXX JJJ", but it shows in the browser "XXX JJJ" (it
hides some spaces).

I tryed to encode it replacing the blanks with   but the dropdownlist
component replace de & with "&" and it makes a desaster.

I don't want to change the dropdownlist web form, any idea???

Any idea to solve this problem?

Thanks,

Pablo Difrieri.
 
Hi Lucas,

Please change the HTML code manually to the following code snippet and test
this issue again. It works on my side.
...
<asp:dropdownlist id="DropDownList1" style="Z-INDEX: 102; LEFT: 288px;
POSITION: absolute; TOP: 64px"
runat="server" Height="120px" Width="208px">
<asp:ListItem Value="Test1"
Selected="True">XXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JJJ</asp:ListItem>
<asp:ListItem Value="test2">test2</asp:ListItem>
</asp:dropdownlist></FONT></form>
...
Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
We had test it and it is shown fine in the browser. The problem is that we
are using the dropdownlist binded to a Dataset, so is difficult to intercept
the render (and we think it could be difficult to do it in all our
dropdownlist controls).

Supposing we unbind the control and add items manually, we'd like to use
codebehind to do it (you can get it in the attach file) because it is part
of the standard we have to apply.

DropDownList1.Items.Add(New ListItem("XXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JJJ",
"Test1"))
DropDownList1.Items.Add(New ListItem("XXX&nbsp;&nbsp;&nbsp;JJJ", "Test2"))

DropDownList1.Items.Add(New ListItem("PPP JJJ", "Test3"))

DropDownList1.Items.Add(New ListItem("PPP JJJ", "Test4"))

In this way, the first two elements encode the &nbsp; and show them
literally. The second two elements eliminates additional spaces.

Any solution to this problem?
The best solution would be that IE doesn't eliminates intermediate blank
spaces. The others are just workarounds.

Thanks a lot.

Pablo Difrieri
 
Hi Lucas,

Please try the following code on your side.
...
Dim myWriter As New StringWriter
Dim myString As String
HttpUtility.HtmlDecode("PPP     JJJ",
myWriter)

myString = myWriter.ToString()
DropDownList1.Items.Add(New ListItem(myString, "Test3"))
...

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
OK. That works for control fulfilled manually.
Can we do something like that using databinding? Is this the cleanest
solution? I mean, the fact that the spaces were replaced, Is the correct /
expected behavior?

Thanks a lot

Pablo Difrieri
 
Hi Lucas,

My solution is the cleanest in the solutions I can find. Based on my
research and experience, I would like to share the following information
with you.

1. In the HTML world, the space is a special character and should be
presented as "&nbsp;" or " ". If we just use some spaces directly in
the HTML code, IE will do some unexpected behavior.

2. For reliable HTTP transmission from the Web server to a client, ASP.NET
converts a string into an HTML-encoded string.

3. In the HTML world, the "&" is also a special character and should be
presented as "&amp;". That is the reason why "&nbsp;" is converted to
"&amp;nbsp;".

In the data binding case, I think, we can make use of the Replace method
from the RegEx class to replace any space inside the values to be bound to
the dropdownlist with " " by iterating the data source before doing
the real binding. Here is the sample code to use the Replace method,

temp = "Miranda " + i.ToString()
rex = New Regex(" ")
temp = HttpUtility.HtmlDecode(rex.Replace(temp, " "))

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
That's OK. The point that it is not clear for me is how can I intercept the
automatic fill of the ListBox that makes the data binding. I understand the
that I need to replace the ordinal spaces for &nbsp; or the 160 character
and how to do it with manually filled ListBoxes, but where must I put the
replacement code you send me when I'm using data binding?

Thanks a lot.

LucasC
 
Hi Lucas,

I am sorry if there is any misunderstanding.

Based on my research and experience, there are two ways to implement the
workaround:

1. Iterate each listitem inside the dropdownlist web control after the
databinding, and replace each space in the text of each item with "&#160".
For example,

DropDownList1.DataSource = dv
DropDownList1.DataTextField = "Employee"
DropDownList1.DataValueField = "EmployeeID"
DropDownList1.DataBind()

Dim myitem As ListItem

For Each myitem In DropDownList1.Items
temp = HttpUtility.HtmlDecode(rex.Replace(myitem.Text,
" "))
myitem.Text = temp
Next

2. Iterate each row of the datatable before binding to the dropdownlist,
and replace each space in each field with "&#160", and then bind the
modified datable to the dropwodnlist.

Please let me know if it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
My last question. Which event allow me to "Iterate each listitem inside the
dropdownlist web control after the databinding"?
Thanks a lot Jacob.

LucasC
 
Hi Lucas,

There is not a special event for this implementation. We can do the
listitem iteration either immediately after the call to the databind()
method, or inside the PreRender event of the dropdownlist web control,
where the databinding has been finished.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top