White-space before Items in DropDownList
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.
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   in .aspx page.
Finally I used “\xA0” as a C# string which is equivalent to “ ” 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.