I have created the checkbox
dynamically in C# with inside the <asp:ContentPlaceHolder>
. I want that to be ordered in vertical and only one box will be checked at a time in code behind. Any solution?
Code:
DataTable table = new DataTable();
table.Columns.Add("Betoption", typeof(string));
table.Columns.Add("id", typeof(string));
table.Rows.Add("Main", "1");
table.Rows.Add("Corner", "2");
table.Rows.Add( "Card & Foul", "3");
table.Rows.Add( "Under / Over", "4");
table.Rows.Add( "Dilantin", "5");
table.Rows.Add( "Home / Away", "6");
table.Rows.Add("First Half", "7");
DataRow[] exemption = table.Select();
foreach (DataRow dr in exemption)
{
string option = dr["Betoption"].ToString();
string optionID = dr["id"].ToString();
var chk = new CheckBox { ID = optionID, Text = option, CssClass = "name", AutoPostBack = true };
PlaceHolder1.Controls.Add(chk);
}
Instead of Checkbox
you can use CheckBoxList
and set value of RepeatDirection
Property as shown below:
var checkList = new CheckBoxList();
checkList.AutoPostBack = true;
checkList.CssClass = "name";
checkList.RepeatDirection = RepeatDirection.Vertical;
foreach (DataRow dr in exemption)
{
string option = dr["Betoption"].ToString();
string optionID = dr["id"].ToString();
checkList.Items.Add(new ListItem()
{
Text = option,
Value = optionID
});
}
PlaceHolder1.Controls.Add(checkList);
And if you want only one item to be checked at a time, I would suggest to use RadioButtonList
instead of Checkbox list.
In above code, Just replace CheckBoxList()
with RadioButtonList()