How to add " " to list item text

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?
 
N

Naveen K Kohli

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
 
T

Tyrone

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");
 
N

Naveen K Kohli

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
 
J

John Timney \(Microsoft MVP\)

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>
 
N

Naveen K Kohli

John,
I did try this before posting the code. Nothing seemed to work. &nbsp; did
not show up as spaces.

Thanks.
 
Joined
Oct 5, 2007
Messages
1
Reaction score
0
Hello, I was having the same problem (trying to add padding to beginning of a dropdown list control to show a hierarchy view) and came upon this post.

After reviewing John Timney's code I was able to get a script together to do this; I hope this helps someone out there.

Code:
' Format chapter navigation in a div hierarchy
Dim padding As String = ""
For Each dr As DataRow In data.Tables(0).Rows
    Dim writer As New System.IO.StringWriter()
    Try
        For i As Integer = 0 To CInt(dr("level")) - 1
            If Not(i = 0) Then padding += "&nbsp;&nbsp;&nbsp;&nbsp;"
        Next
    Catch
        padding = ""
    End Try
    Server.HtmlDecode(padding, writer)
    padding = writer.ToString()
    ddlChapterNavigation.Items.Add( New ListItem((padding & dr("desc").ToString), dr("id").ToString) )
    writer.Dispose() : padding = ""
Next
ddlChapterNavigation.SelectedValue = Request.QueryString("id").ToString()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top