Reputation: 203
I need your help with this one, I don't know if it is possible.
Imagine a RadioButtonList populated from a Database using a databind from vb codebehind looking like this;
[1] - Apple - Fruit
[2] - Banana - Fruit
[3] - Beetroot - Vegetable
[4] - Orange - Fruit
[5] - Strawberry - Fruit
[6] - Carrot - Vegetable
[7] - Potato - Vegetable
Now I want to make it look like this;
- [Fruit]
- Apple
- Banana
- Orange
- Strawberry
- [Vegetable]
- Beetroot
- Carrot
- Potato
I think I am able to group the radio button somehow or change the label. I also thought about using a tree node which I have not used before.
Upvotes: 1
Views: 128
Reputation: 203
OK you can not use attributes on radio buttons or list box items. The attribute is added to the parent only. You can achieve this by rewriting the DOM I have seen on another forum, requires a lot of the work I just will create 2 radio buttons to get me going. Its funny because if I was using PHP I could call a radio button list or select list and populate whatever attribute I wanted. Well Done Microsoft for removing this common use function! Just like the onkeyup events and ondblclick are only available client side.
Upvotes: 0
Reputation: 1790
if use webform read this article: https://weblogs.asp.net/alaaalnajjar/group-options-in-dropdownlist
if your ptoject is mvc you must use selected list:
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public static List<Car> GetCars()
{
return new List<Car>{
new Car{Id=1, Name="Volvo", Category="Swedish Cars"},
new Car{Id=2, Name="Mercedes-Benz", Category="German Cars"},
new Car{Id=3, Name="Saab", Category="Swedish Cars"},
new Car{Id=4, Name="Audi", Category="German Cars"},
new Car{Id=5, Name="Audi", Category="German Cars"},
};
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.CarsList = GetCarsSelectList();
return View();
}
private SelectList GetCarsSelectList()
{
return new SelectList(Car.GetCars(), "Id", "Name","Category",1);
}
}
Upvotes: 2