本文介绍了在asp.net c#中进行在线测验需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 伙计们, 我发现了一个非常足智多谋的在线测验实现。 在线测验 [ ^ ] 但问题是我希望重用此代码并用C#写的代码在表单后面。 我一直试图转换但最终出现了大量错误。如果有人能弄明白的话会很优雅。 我非常感激。 背后的代码,quiz.aspx.csGuys ,I found an very resourceful implementation of online quiz .Online Quiz[^]But the problem is i wish to reuse this code and written in C# with code behind form.I have been trying to convert but end up with ton of error. will be graceful if any one can figure it out.I be deeply appreciate .the code behind ,quiz.aspx.csusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;using System.Xml.XPath;public partial class _Default : System.Web.UI.Page{ //Relative file path to XML data string strXmlFilePath = Server.MapPath("quiz.xml"); XPathDocument xDoc = new XPathDocument(strXmlFilePath); XPathNavigator xNav = xDoc.CreateNavigator; XPathNodeIterator xNodeIterator; //Initialize variables int intTotalQuestion; int intQuestionNo = 1; int intScore = 0; ArrayList arrAnswerHistory = new ArrayList(); public void Page_Load(object sender, EventArgs e) { //Start a new quiz? if (!Page.IsPostBack) { //Yes! Count total question intTotalQuestion = xNav.Select("/quiz/mchoice").Count; //Record start time ViewState["StartTime"] = DateTime.Now; ShowQuestion(intQuestionNo); } } public void btnSubmit_Click(object src, EventArgs e) { //Retrieve essential variables from state bag intTotalQuestion = ViewState["TotalQuestion"]; intQuestionNo = ViewState["QuestionNo"]; intScore = ViewState["Score"]; arrAnswerHistory = ViewState["AnswerHistory"]; //Correct answer? if (rblAnswer.SelectedItem.Value == ViewState["CorrectAnswer"]) { intScore += 1; arrAnswerHistory.Add(0); } else { arrAnswerHistory.Add(rblAnswer.SelectedItem.Value); } //End of quiz? if (intQuestionNo == intTotalQuestion) { //Yes! Show the result... QuizScreen.Visible = false; ResultScreen.Visible = true; //Render result screen ShowResult(); } else { //Not yet! Show another question... QuizScreen.Visible = true; ResultScreen.Visible = false; intQuestionNo += 1; //Render next question ShowQuestion(intQuestionNo); } } public void ShowQuestion(int intQuestionNo) { string strXPath = null; int intLoop = 0; TimeSpan objTimeSpent = default(TimeSpan); strXPath = "/quiz/mchoice[" + intQuestionNo.ToString() + "]"; //Extract question xNodeIterator = xNav.Select(strXPath + "/question"); xNodeIterator.MoveNext(); lblQuestion.Text = intQuestionNo.ToString() + ". " + xNodeIterator.Current.Value; //Extract answers xNodeIterator = xNav.Select(strXPath + "/answer"); //Clear previous listitems rblAnswer.Items.Clear(); intLoop = 0; while (xNodeIterator.MoveNext()) { intLoop += 1; //Add item to radiobuttonlist rblAnswer.Items.Add(new ListItem(xNodeIterator.Current.Value, intLoop)); //Extract correct answer if (xNodeIterator.Current.GetAttribute("correct", "") == "yes") { ViewState["CorrectAnswer"] = intLoop; } } //Output Total Question lblTotalQuestion.Text = intTotalQuestion; //Output Time Spent objTimeSpent = DateTime.Now.Subtract(ViewState["StartTime"]); lblTimeSpent.Text = objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString(); //Store essential data to state bag ViewState["TotalQuestion"] = intTotalQuestion; ViewState["Score"] = intScore; ViewState["QuestionNo"] = intQuestionNo; ViewState["AnswerHistory"] = arrAnswerHistory; } public void ShowResult() { string strResult = null; int intCompetency = 0; int intLoop = 0; string strXPath = null; TimeSpan objTimeSpent = default(TimeSpan); objTimeSpent = DateTime.Now.Subtract(ViewState["StartTime"]); strResult = "<center>"; strResult += "<h3>Quiz Result</h3>"; strResult += "<p>Points: " + intScore.ToString() + " of " + intTotalQuestion.ToString(); strResult += "<p>Your Competency: " + Conversion.Int(intScore / intTotalQuestion * 100).ToString() + "%"; strResult += "<p>Time Spent: " + objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString(); strResult += "</center>"; strResult += "<h3>Quiz Breakdown:</h3>"; for (intLoop = 1; intLoop <= intTotalQuestion; intLoop++) { strXPath = "/quiz/mchoice[" + intLoop.ToString() + "]"; xNodeIterator = xNav.Select(strXPath + "/question"); xNodeIterator.MoveNext(); strResult += "<b>" + intLoop.ToString() + ". " + xNodeIterator.Current.Value + "</b><br>"; if (arrAnswerHistory[intLoop - 1] == 0) { strResult += "<font color=\"green\"><b>Correct</b></font><br><br>"; } else { xNodeIterator = xNav.Select(strXPath + "/answer[" + arrAnswerHistory[intLoop - 1].ToString() + "]"); xNodeIterator.MoveNext(); strResult += "<b>You answered:</b> " + xNodeIterator.Current.Value + "<br>"; strResult += "<font color=\"red\"><b>Incorrect</b></font><br><br>"; } } lblResult.Text = strResult; }} quiz.aspx。the quiz.aspx.<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Australian Geography Quiz</title></head><style>body { font-size: 10pt; font-family: verdana,helvetica,arial,sans-serif; color:#000000; background-color:#eeeedd;}tr.heading { background-color:#900B08;}.button { border: 1px solid #000000; background-color: #ffffff;}</style><body><span id="QuizScreen" runat="server"><form id="Form2" runat="server"><table width="100%" border="0" cellpadding="2" cellspacing="0"> <tr class="heading"> <td width="50%"><font color="white"><b>Australian Geography Quiz</b></font></td> <td width="50%" align="right"><font color="white"><b>www.codeproject.com</b></font></td> </tr> <tr> <td colspan="2"> <b><asp:label id="lblQuestion" runat="server" /></b><br> <asp:radiobuttonlist id="rblAnswer" RepeatDirection="vertical" TextAlign="right" RepeatLayout="table" runat="server" /><br> <asp:requiredfieldvalidator ID="Requiredfieldvalidator1" ControlToValidate="rblAnswer" ErrorMessage="Please pick an answer!" runat="server" /><br> <asp:button id="btnSubmit" class="button" text=" Next " onClick="btnSubmit_Click" runat="server" /> </td> </tr> <tr class="heading"> <td width="50%"><font color="white"><b>Total <asp:label id="lblTotalQuestion" runat="server" /> questions</b></font></td> <td width="50%" align="right"><font color="white"><b>Time spent <asp:label id="lblTimeSpent" runat="server" /></b></font></td> </tr></table></form></span><span id="ResultScreen" runat="server"><asp:label id="lblResult" runat="server" /></span></body></html quiz.xml来自在线测验 [ ^ ]the quiz.xml originated from Online Quiz[^]<<pre lang="xml">?xml version="1.0" encoding="UTF-8"?><!-- 10 question quiz about Australian Geography --><quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="quiz.xsd"> <mchoice> <question>What is the capital city of Australia?</question> <answer>Sydney</answer> <answer correct="yes">Canberra</answer> <answer>Melbourne</answer> <answer>Gold Coast</answer> </mchoice> <mchoice> <question>Launceston is the second largest city in which Australian state?</question> <answer>Victoria</answer> <answer>New South Wales</answer> <answer correct="yes">Tasmania</answer> <answer>Western Australia</answer> </mchoice> <mchoice> <question>Which state has the famous 'Twelve Apostles' ?</question> <answer correct="yes">Victoria</answer> <answer>South Australia</answer> <answer>New South Wales</answer> <answer>Western Australia</answer> </mchoice> <mchoice> <question>Which is a popular ski resort in NSW?</question> <answer correct="yes">Perisher Blue</answer> <answer>Mt. Buller</answer> <answer>Mt. Baw-Baw</answer> <answer>Lake Mountain</answer> </mchoice> <mchoice> <question><![CDATA[Which of the following is <u>NOT</u> Australian native animals?]]></question> <answer>Kangaroo</answer> <answer correct="yes">Penguin</answer> <answer>Koala</answer> <answer>Wombat</answer> </mchoice> <mchoice> <question>Which city has an extensive tram network?</question> <answer>Sydney</answer> <answer correct="yes">Melbourne</answer> <answer>Adelaide</answer> <answer>Ballarat</answer> </mchoice> <mchoice> <question>What is known as 'The Silver City' in Australia?</question> <answer>Alice Springs</answer> <answer correct="yes">Broken Hill</answer> <answer>Ballarat</answer> <answer>Silverton</answer> </mchoice> <mchoice> <question>In which location the war movie 'Thin Red Line' was taken?</question> <answer>Anglesea</answer> <answer>Apollo Bay</answer> <answer>Margaret River</answer> <answer>Monkey Mia</answer> <answer correct="yes">Townsville</answer> </mchoice> <mchoice> <question><![CDATA[Which is <u>NOT</u> true about Uluru ?]]></question> <answer>It is the world biggest monolith located in the centre of Australian continent</answer> <answer>It was named 'Ayers Rock' by European explorer William Gosse in 1873</answer> <answer correct="yes">Aboriginal people encourage tourists to climb Uluru</answer> <answer>The area contains carvings and paintings by Aboriginal people</answer> </mchoice> <mchoice> <question>What is so special about Longreach?</question> <answer>The place where a blacksmith named Thomas Hiscock found the first gold that triggerred gold rush</answer> <answer>The town has an expansive, well-preserved penal colony of Australia's early history</answer> <answer correct="yes">The first commercial flight by Qantas took from this town in 1921</answer> <answer>None of these answers are correct</answer> </mchoice></quiz>推荐答案 这篇关于在asp.net c#中进行在线测验需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-07 03:28