Skip to content

White-space before Items in DropDownList

12 Aug, 12

The Problem:

In a restaurant, there are some categories of food items available like Pizza, Burger etc. Each Category is having various food items. The food items are having furthers sub levels.
While selecting a category from drop down (not shown in figure), the food item dropdown must be populated with all  food items under selected category. Let’s say if Pizza is selected in category dropdown (not shown in figure), then all food items of category Pizza will be displayed in food item dropdown as shown in below screenshot. Here there are  two sublevel at level 1: Veg Pizza and Non-Veg Pizza. At level 2, under Veg Pizza,  we have further two child level as “Paneer Pizza” and “Cheese Pizza”. The Paneer Pizza is again have two sub-level as Peppy Paneer and Spicy Paneer.

So here we need to add space before the sublevels depending on its level while adding an item in dropdown list as shown above.

The Solution:

To implement the requirement, I have tried String.Format(“ ”+FoodItemName) and many other ways for all space before sub levels but dropdown was trimming it the while space while adding items. Since we are getting list of food items at runtime, we need to do it using code behind only so no scope of using &nbsp in .aspx page.
Finally I used “\xA0” as a C# string which is equivalent to “&nbsp” in html. The whole solution is like:
(click on image for clear and big view)

Here at line # 24, we are adding the space for sublevel and using recursion to add further sublevels.
In line # 26, after recursion call, we are removing extra padding.

Thanks for reading. Happy Coding.


From → ASP.NET

  1. Jyothsna permalink

    Thanks a lot. Many blogs i went thorugh but ur “\xA0” showed me the way…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: