Reputation: 1853
I have a View that is meant to display a question and several radio buttons representing possible answers to the question. The number of possible answers changes with each question, so I need to find a way of generating the radio buttons dynamically. I am new to aspx syntax and am not quite sure how to go about this, i.e. how to I display the collection of radiobuttons I created in the script in the html below? Would using RadioButtonList be better for this?
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<UncleBobWebService.Models.MultipleChoiceQuestion>" %>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Application</title>
<link href="/Theme/UncleBobTheme.css" rel="Stylesheet" />
<script lang="cs" runat="server">
protected void Page_Load(Object o, EventArgs e)
{
int count = Model.PossibleAnswers.Count;
List<RadioButton> radioButtons = new List<RadioButton>();
for (int i = 0; i < count; ++i)
{
RadioButton button = new RadioButton();
button.ID = "1";
button.GroupName = "answers";
button.Text = Model.PossibleAnswers[i+1].TheAnswer;
button.Checked = false;
radioButtons.Add(button);
}
}
</script>
</head>
<body>
<h2>Question</h2>
<div class="body">
<form id=form1 runat=server action="/ApplicantApply/FormAction">
<div class="box">
<div class="left-justify">
<div><%= Html.DisplayFor(Model => Model.Question) %></div>
<!-- How to display the radiobutton list here instead of below? -->
<input type="radio" id="yesAnswer" name="yes" value="Yes">Yes<br />
<input type="radio" id="noAnswer" name="no" value="No">No<br />
</div>
</div>
<input type="submit" name="submit" value="Previous" />
<input type="submit" name="submit" value="Next" />
</form>
</div>
</body>
</html>
Upvotes: 1
Views: 9824
Reputation: 1
display answer and question on page.but the question answer display option type wise and the option type is multipal choice single answer,multipal answer multipal choice,image date. if answer tabel option type field set if set 1 so display data in radio button wise in gridview to so to create this page in asp.net
Upvotes: 0
Reputation: 1409
Seems like you're mixing ASP.NET & ASP.NET MVC
If you're Using ASP.NET MVC:
<% for (int i = 0; i < Model.PossibleAnswers.Count; ++i) { %>
{
<label>
<%= Html.RadioButtonFor(m => m.PossibleAnswers[i].TheAnswer, m.PossibleAnswers[i]..ID) m.PossibleAnswers[i].TheAnswer %>
</label>
<% } %>
If you are using ASP.NET:
Use RadioButtonList
for this purpose. It is better suited for such operations.
One way could be to declare the control in your aspx file and then on PageLoad
event you can add items/bind it to the collection.
IMHO Binding is a generally a better option.
Binding Example:
http://asp-net-example.blogspot.com/2009/03/how-data-bind-radiobuttonlist-on-button.html
Adding Example:
protected override void OnInit(EventArgs e)
{
RadioButtonList1.Items.AddRange(GetItems());
base.OnInit(e);
}
private ListItem[] GetItems()
{
return new ListItem[] {
new ListItem("Item 1", "1"),
new ListItem("Item 2", "2"),
new ListItem("Item 3", "3"),
new ListItem("Item 4", "4")
});
}
Upvotes: 1
Reputation: 2296
You're mixing Asp.net WebForm example code with an Asp.net MVC view. You don't want to use Page_Load or any Asp.net server controls in MVC. You want to use Html.RadioButton()
<div class="left-justify">
<div><%= Html.DisplayFor(Model => Model.Question) %></div>
<% for (var i=i; i<=Model.PossibleAnswers.Count(); i++) %>
<label><%= Html.RadioButtonFor(m => m.PossibleAnswers[i], "Yes")> %> Yes</label>
<label><%= Html.RadioButtonFor(m => m.PossibleAnswers[i], "No")> %> No</label>
<% } %>
</div>
These things are called Html Helpers by asp.net. You can google that term to find examples of them. They exist for most of the common form elements.
Upvotes: 1
Reputation: 84
You can use the below links.
http://www.developerscode.com/2011/02/how-to-create-dynamic-registration-form.html
dynamically created radiobuttonlist
http://csharp-guide.blogspot.in/2012/05/adding-radiobutton-options-dynamically.html
http://forums.asp.net/t/1192583.aspx/1
http://www.codeproject.com/Questions/360219/radio-button-dynamic-creation
ASP.net creating dynamic Radio Button List
Hope this will help you.
Upvotes: 0