如何修复此错误以防止返回测验页面并更改他的答案
本文关键字:答案 错误 何修复 返回 | 更新日期: 2023-09-27 18:21:46
我现在正在开发的web应用程序有一个叫做测验引擎的东西,它为用户提供由一个或多个问题组成的简短测验。现在,我在参加/回答测验时遇到了一个问题:当用户完成由4个问题组成的测验时,他进入结果页面,他可以返回(使用浏览器中的后退箭头)到测验页面,再次回答任何问题,这不应该发生,我不知道如何防止
为了创建测验引擎,我在ASP.NET网站中使用了Toturial For the Quiz engine来创建我所拥有的东西。
ASP.NET代码:
<tr>
<td>
<asp:DetailsView ID="questionDetails" runat="server" Height="50px" Width="550px" AutoGenerateRows="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="80px" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="Question" HeaderText="Question:" SortExpression="Question" />
<asp:BoundField DataField="Answer1" HeaderText="A:" SortExpression="Answer1" />
<asp:BoundField DataField="Answer2" HeaderText="B:" SortExpression="Answer2" />
<asp:BoundField DataField="Answer3" HeaderText="C:" SortExpression="Answer3" />
<asp:BoundField DataField="Answer4" HeaderText="D:" SortExpression="Answer4" />
</Fields>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="generaltext" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [QuestionID], [Question], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [QuestionOrder] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder]">
<SelectParameters>
<asp:SessionParameter SessionField="QuizID" Type="Int32" Name="QuizID" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
<%--<tr>
<td>
</td>
</tr>--%>
<tr>
<td class="boldtext">
<strong>Your Answer:</strong>
<asp:DropDownList ID="answerswerDropDownList" runat="server">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
我的代码背后:
这是负责保存答案的代码:
protected void Page_Load(object sender, EventArgs e)
{
questionDetails.DataBind();
answerDropDownList.SelectedIndex = 0;
if (questionDetails.PageCount == 1)
{
nextButton.Text = "Finished";
}
}
protected void nextButton_Click(object sender, EventArgs e)
{
// Save off previous answers
System.Data.DataRowView dr = (System.Data.DataRowView)questionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionOrder"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = answerDropDownList.SelectedValue.ToString();
ArrayList al = (ArrayList)Session["AnswerList"];
var oldAnswer = al.ToArray().Where(ans => (ans as Answer).QuestionID == a.QuestionID);
if (oldAnswer.Count() != 0)
{
a = oldAnswer.FirstOrDefault() as Answer;
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = answerDropDownList.SelectedValue.ToString();
}
else
{
al.Add(a);
}
if (questionDetails.PageIndex == questionDetails.PageCount - 1)
{
// Go to evaluate answers
Response.Redirect("Results.aspx");
}
else
{
questionDetails.PageIndex++;
}
if (questionDetails.PageIndex == questionDetails.PageCount - 1)
{
nextButton.Text = "Finished";
}
}
以下代码负责保存结果:
protected void Page_Load(object sender, EventArgs e)
{
ArrayList al = (ArrayList)Session["AnswerList"];
if (al == null)
{
Response.Redirect("default.aspx");
}
resultGrid.DataSource = al;
resultGrid.DataBind();
// Save the results into the database.
if (IsPostBack == false)
{
// Calculate score
double questions = al.Count;
double correct = 0.0;
for (int i = 0; i < al.Count; i++)
{
Answer a = (Answer)al[i];
if (a.Result == Answer.ResultValue.Correct)
correct++;
}
double score = (correct / questions) * 100;
string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO''", "");
SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (@QuizID, @DateTimeComplete, @Score, @Username)";
userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
// "N4" is for displaying four decimal places, regardless of what the value is
userQuizDataSource.InsertParameters.Add("Score", score.ToString("N4"));
userQuizDataSource.InsertParameters.Add("Username", username);
int rowsAffected = userQuizDataSource.Insert();
if (rowsAffected == 0)
{
// Let's just notify that the insertion didn't
// work, but let' s continue on ...
errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu.";
}
}
}
protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
}
那么,现在我如何防止用户在结果页面中时返回测验
您可以使用Response.Cache属性来解决此问题。来自MSDN响应。SetAllowResponseInBrowserHistory
当HttpCacheability设置为NoCache或ServerAndNoCache时Expires HTTP标头默认设置为-1;这个告诉客户不要在"历史记录"文件夹中缓存响应,以便在使用后退/前进按钮客户端请求新版本的响应每次。您可以通过调用带有allow参数集的SetAllowResponseInBrowserHistory方法真实。
在页面加载方法中,您可以添加此行。
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetAllowResponseInBrowserHistory(false);
// or else you can do like this
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = -1;
Response.CacheControl = "No-cache";
}