我建立了一个 ASP.NET 网站,该网站进行在线考试并显示分数。我在本地发布并托管在 IIS 中。然后我通过局域网连接了我的系统与另一个问题。我用笔记本电脑回答了前 5 个问题。当我尝试从另一台笔记本电脑访问该网站时,它从第 5 个问题而不是第一个问题开始。
public partial class MainPage : System.Web.UI.Page
{
public static int Qnum=1, Score=0,Count=0;
public static List<int> answered=new List<int>();
private SQLconnect sqlconnect = new SQLconnect();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Score"] = Score;
fnMCq();
}
}
public void fnMCq()
{
try
{
DataTable dt = new DataTable();
sqlconnect.Connection_string = System.Configuration.ConfigurationManager.AppSettings["conn_MCQ"];
string query = "select * from Questions where qid='" + Qnum + "'";
dt = sqlconnect.getDataTable(query);
string[] array = dt
.AsEnumerable()
.Select(row => row.Field<string>("question"))
.ToArray();
Question.Text = array[0];
query = "Select * from Answers where qid='" + Qnum + "'";
dt.Clear();
dt = sqlconnect.getDataTable(query);
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add(dt.Rows[0][1].ToString(), "1");
values.Add(dt.Rows[0][2].ToString(), "2");
values.Add(dt.Rows[0][3].ToString(), "3");
values.Add(dt.Rows[0][4].ToString(), "4");
Answers.DataSource = values;
Answers.DataTextField = "key";
Answers.DataValueField = "Value";
Answers.DataBind();
fnHideSubmit();
if (Qnum == 1)
btnPrevious.Enabled = false;
else
btnPrevious.Enabled = true;
if (Qnum == 20)
btnNext.Enabled = false;
else
btnNext.Enabled = true;
}
catch (Exception e)
{
Session["error"] = e.Message;
Response.Redirect("ErrorPage.aspx");
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
if (!isexamover())
{
Qnum = Qnum - 1;
fnMCq();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
if (!isexamover())
{
Qnum = Qnum + 1;
fnMCq();
}
}
public void fnUpdateScore()
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn_MCQ"]);
int selectedanswer,answer;
selectedanswer=Convert.ToInt32(Answers.SelectedValue);
string query = "select answer from questions where qid='"+Qnum+"'";
SqlCommand cmd=new SqlCommand(query,conn);
conn.Open();
sqlconnect.Connection_string = System.Configuration.ConfigurationManager.AppSettings["conn_MCQ"];
answer =Convert.ToInt32( cmd.ExecuteScalar().ToString()) ;
if (answer == selectedanswer)
{
Score = Score + 1;
Session["Score"] = Score;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
answered.Add(Qnum);
fnHideSubmit();
fnUpdateScore();
if (!qbound())
{
if (!isexamover())
{
++Qnum;
fnMCq();
}
else
{
Session["Score"] = Score;
Response.Redirect("ScoreDisplay.aspx");
}
}
}
public void fnHideSubmit()
{
foreach (int i in answered)
{
if (i == Qnum)
{
btnSubmit.Enabled = false;
break;
}
else
btnSubmit.Enabled = true;
}
}
public bool qbound()
{
if (Qnum == 1 || Qnum == 20)
return true;
else
return false;
}
public bool isexamover()
{
if (answered.Count == 20)
{
Session["Score"] = Score;
Response.Redirect("ScoreDisplay.aspx");
return true;
}
return false;
}