How to add " " to list item text

  • Thread starter Thread starter Naveen K Kohli
  • Start date Start date
N

Naveen K Kohli

I am try to set the text of the drop down list item as

ListItem li = new ListItem();
li.Text = " "+"MyValue";

myDropDown.Items.Add("li);

The intent is to add a spacing in front of the text. Its not doing what I am
trying to do. The text shows up as "&ampMyValue"

How can this be accomplished?
 
Thanks Justin,
I tried that. It truncates the blank spaces at the start. In regular ASP
apps I was able to do..

<option>&nbsp;&nbsp;MyValue

I am trying to do the same thing with DropdownList control
 
What I think the issue is is that you are using the "+" to
add both strings..i think you must use the & char.

Well in VB.Net in C# your right on the money I think..you
use the "+"..but you didn't specify any language

ListItem li = new ListItem();
li.Text = "&nbsp;" & "MyValue";

myDropDown.Items.Add("li");
 
I think choice of language was clear. I don't think that VB uses ";" at the
end of statement. Thats not the point.
ListItem truncates the leading spaces. And if you have any HTML decoded
string, it will encode it and display as "&ampndp;". The solution to this
issue can be found at floowing link.,

http://www.netomatix.com/IndentDropdownList.aspx

Naveen
 
The code below should help you out.
--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------

<%@ Page Language="VB" Debug="true" %>
<script language=VB runat=server>

Sub Page_Load(Sender As Object, E As EventArgs)

Dim Padding As String
Dim writer As New System.IO.StringWriter()
Dim DecodedString As String

Padding = "&nbsp;&nbsp;"
Server.HtmlDecode(Padding, writer)
Padding = writer.ToString()

myDropDownList2.Items.Add(Padding & " MVP's")

Padding = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
Server.HtmlDecode(Padding, writer)
Padding = writer.ToString()

myDropDownList2.Items.Add(Padding & " MVP's")

myDropDownList2.Items.Add("MVP's" & Padding & "Do Their best")

End Sub

</script>
<html>
<head><title>Padding DropListBox</title></head>
<body><form fred runat="server"><asp:dropdownlist id="MyDropDownList2"
runat="server"/></form></body>
</html>
 
John,
I did try this before posting the code. Nothing seemed to work. &nbsp; did
not show up as spaces.

Thanks.
 
Back
Top