C# 如何修复此错误以防止返回测验页面并更改其答案?
我现在正在开发的web应用程序有一个叫做测验引擎的东西,它为用户提供由一个或多个问题组成的简短测验。现在,我在参加/回答测验时遇到了一个问题: 当用户完成由4个问题组成的测验后,他进入结果页面,他可以返回(使用浏览器中的返回箭头)测验页面并再次回答任何问题,这不应该发生,我不知道如何防止 为了创建测验引擎,我使用ASP.NET网站中测验引擎的Toturial来创建我所拥有的内容 ASP.NET代码:C# 如何修复此错误以防止返回测验页面并更改其答案?,c#,asp.net,C#,Asp.net,我现在正在开发的web应用程序有一个叫做测验引擎的东西,它为用户提供由一个或多个问题组成的简短测验。现在,我在参加/回答测验时遇到了一个问题: 当用户完成由4个问题组成的测验后,他进入结果页面,他可以返回(使用浏览器中的返回箭头)测验页面并再次回答任何问题,这不应该发生,我不知道如何防止 为了创建测验引擎,我使用ASP.NET网站中测验引擎的Toturial来创建我所拥有的内容 ASP.NET代码: <tr> <td>
<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="answerDropDownList" 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)
{
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;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
ArrayList al=(ArrayList)会话[“应答列表”];
if(al==null)
{
重定向(“default.aspx”);
}
resultGrid.DataSource=al;
resultGrid.DataBind();
//将结果保存到数据库中。
如果(IsPostBack==false)
{
//计分
双重问题=全部计数;
双精度=0.0;
对于(int i=0;i
那么现在我如何防止用户在结果页时返回测验?您可以使用Response.Cache属性来解决此问题。 来自MSDN 当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";
}
这与您当前的位置相比将是一个很大的进步,但是如果您的页面使用AJAX动态加载问题,并且您从后面的代码控制测验流,那么用户将无法返回。或者,在向用户显示结果之前,您可以将答案放在数据库的末尾,然后检查答案是否存在,然后再让他们对测验进行评分。我不使用AJAX。这些问题是从数据库中加载的。在代码隐藏中是否有修复此问题的方法?
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";
}