I am trying to put together a polling application, I need to display a radio button list with options pulled from a database (Option_1, Option_2, etc) but I need the radio button value to be 1, 2, 3, 4 as when it gets submitted it will get inserted into another database that holds the poll results. I have been playing around with this code here and I can't get the Option text to show next to the Radio button. Any suggestions?
<asp:RadioButtonList ID="Answers" runat="server">
<asp:ListItem Value="1" Text="<% prs['Option_1'].ToString() %>"></asp:ListItem>
<asp:ListItem Value="2" Text="<% prs['Option_2'].ToString() %>"></asp:ListItem>
<asp:ListItem Value="3" Text="<% prs['Option_3'].ToString() %>"></asp:ListItem>
<asp:ListItem Value="4" Text="<% prs['Option_4'].ToString() %>"></asp:ListItem>
</asp:RadioButtonList>
also messed around with this
var op1 = prs["Option_1"].ToString();
var op2 = prs["Option_2"].ToString();
var op3 = prs["Option_3"].ToString();
var op4 = prs["Option_4"].ToString();
if (op1 != "")
{
Answers.Items.Add("1");
}
if (op2 != "")
{
Answers.Items.Add("2");
}
if (op3 != "")
{
Answers.Items.Add("3");
}
if (op4 != "")
{
Answers.Items.Add("4");
}
Here is one way to do it. Replace the for loop with a loop going over the answers you want to display. Create a ListItem for each item and put that into a list. Then set the list as the Datasource for the radio button list.
var optionList = new List<ListItem>();
for (var i = 0; i < 4; i++)
{
var newItem = new ListItem()
{
Value = count.ToString(),
Text = string.Format("Option {1}", count.ToString());
};
optionList.Add(newItem);
}
Answers.DataSource = optionList;
Answers.DataBind();