i have created multiple choice quiz in asp code have long, know way of making shorter. have validators each of inputs im not sure how can stop submit button being clickable unless of answers input.
my test code:
<%@ page title="" language="c#" masterpagefile="~/synthermaster.master" autoeventwireup="true" codebehind="moments , energy test.aspx.cs" inherits="computingproject.revisionpages._4._nuclear_and_particle_physics.nuclear_and_particle_physics_topic_list" %> <asp:content id="content2" contentplaceholderid="maincontent" runat="server"> <asp:label id="qlabel1" runat="server" text="question 1"></asp:label> <asp:radiobuttonlist id="radiobuttonlist1" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q1requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist1" errormessage="ensure question 1 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel2" runat="server" text="question 2"></asp:label> <asp:radiobuttonlist id="radiobuttonlist2" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q2requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist2" errormessage="ensure question 2 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel3" runat="server" text="question 3"></asp:label> <asp:radiobuttonlist id="radiobuttonlist3" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q3requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist3" errormessage="ensure question 3 completed"></asp:requiredfieldvalidator> </div> <asp:label id="qlabel4" runat="server" text="question 4"></asp:label> <asp:radiobuttonlist id="radiobuttonlist4" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <div> <asp:requiredfieldvalidator id="q4requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist4" errormessage="ensure question 4 completed"></asp:requiredfieldvalidator> <asp:label id="qlabel5" runat="server" text="question 5"></asp:label> <asp:radiobuttonlist id="radiobuttonlist5" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="q5requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist5" errormessage="ensure question 5 completed"></asp:requiredfieldvalidator> <asp:label id="qlabel6" runat="server" text="question 6"></asp:label> <asp:radiobuttonlist id="radiobuttonlist6" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="q6requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist6" errormessage="ensure question 6 completed"></asp:requiredfieldvalidator> <asp:label id="qlabel7" runat="server" text="question 7"></asp:label> <asp:radiobuttonlist id="radiobuttonlist7" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="q7requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist7" errormessage="ensure question 7 completed"></asp:requiredfieldvalidator> <asp:label id="qlabel8" runat="server" text="question 8"></asp:label> <asp:radiobuttonlist id="radiobuttonlist8" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="q8requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist8" errormessage="ensure question 8 completed"></asp:requiredfieldvalidator> <asp:label id="qlabel9" runat="server" text="question 9"></asp:label> <asp:radiobuttonlist id="radiobuttonlist9" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="q9requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist9" errormessage="ensure question 9 completed"></asp:requiredfieldvalidator> <asp:label id="qlabel10" runat="server" text="question 10" ></asp:label> <asp:radiobuttonlist id="radiobuttonlist10" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="q10requiredvalidator" runat="server" display="dynamic" controltovalidate="radiobuttonlist10" errormessage="ensure question 10 completed"></asp:requiredfieldvalidator> <asp:button id="button1" runat="server" text="submit final answers" onclick="submit_click" visible="true" /> <script runat="server"> protected void submit_click(object sender, eventargs e) { int score = 0; list<radiobuttonlist> list = new list<radiobuttonlist>() { radiobuttonlist1, radiobuttonlist2, radiobuttonlist3, radiobuttonlist4, radiobuttonlist5, radiobuttonlist6, radiobuttonlist7, radiobuttonlist8, radiobuttonlist9, radiobuttonlist10 }; foreach (var element in list) { if (element.selectedvalue == "correct") { score++; } } response.write("you scored: "+score); button1.visible = false; } </script> </asp:content>
sorry state of im quite new , im trying best learn , code @ same time.
you use data control such listview, gridview. won't have change single line of code if have 100 questions.
<asp:listview runat="server" id="listview1"> <itemtemplate> <%# eval("question") %> <asp:hiddenfield runat="server" id="idhiddenfield" value='<%# eval("id") %>'/> <asp:radiobuttonlist id="answerradiobuttonlist" runat="server" repeatdirection="horizontal" repeatlayout="table"> <asp:listitem text="*answer 1*" value="incorrect 1" /> <asp:listitem text="*answer 2*" value="correct" /> <asp:listitem text="*answer 3*" value="i" /> <asp:listitem text="*answer 4*" value="4" /> </asp:radiobuttonlist> <asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" display="dynamic" controltovalidate="answerradiobuttonlist" errormessage="ensure question completed" cssclass="field-validation-error" /> <hr /> </itemtemplate> </asp:listview> <asp:button id="button1" runat="server" text="submit final answers" onclick="submit_click" visible="true" />
code behind
public class quiz { public int id { get; set; } public string question { get; set; } public string answer { get; set; } } public partial class webform2 : system.web.ui.page { // come database private ilist<quiz> quizzes { { return new list<quiz> { new quiz {id = 1, question = "question 1"}, new quiz {id = 2, question = "question 2"}, new quiz {id = 3, question = "question 3"}, new quiz {id = 4, question = "question 4"}, new quiz {id = 5, question = "question 5"} }; } } protected void page_load(object sender, eventargs e) { if (!ispostback) { listview1.datasource = quizzes; listview1.databind(); } } protected void submit_click(object sender, eventargs e) { ilist<quiz> quizzes = new list<quiz>(); (int = 0, length = quizzes.count; < length; i++) { listviewdataitem item = listview1.items[i]; var radiobuttonlist = item.findcontrol("answerradiobuttonlist") radiobuttonlist; var idhiddenfield = item.findcontrol("idhiddenfield") hiddenfield; quizzes.add(new quiz { id = convert.toint32(idhiddenfield.value), answer = radiobuttonlist.selectedvalue }); } } }
Comments
Post a Comment