The following code block below will allow you to display spacing or indention in the DropDownList items.
protected void Page_Load(object sender, EventArgs e){
if (!Page.IsPostBack)
{
const string spaceChar = " ";
DropDownList1.Items.Add(new ListItem("Parent Item 1", "Parent Item 1"));
DropDownList1.Items.Add(new ListItem("Parent Item 2", "Parent Item 2"));
DropDownList1.Items.Add(new ListItem(Server.HtmlDecode(spaceChar + "Sub1 Item 2"), "Sub1 Item 2"));
DropDownList1.Items.Add(new ListItem(Server.HtmlDecode(spaceChar + "Sub2 Item 2"), "Sub2 Item 2"));
DropDownList1.Items.Add(new ListItem(Server.HtmlDecode(spaceChar + spaceChar + "Sub1 Item 3"), "Sub1 Item 3"));
DropDownList1.Items.Add(new ListItem("Parent Item 3", "Parent Item 3"));
}
}
As you can see we just Decode the character the HTML equivalent for Space and then append it to the actual ListItems of the DropDownList..
See the sample output below:
That's it! Hope you will find this example useful!
Technorati Tags:
ASP.NET,
C#,
TipsTricks