如何编写在线考试中的下一个按钮的代码以及前一个按钮的代码 [英] how to write code for next button in online examination and also for previous button
问题描述
我已经编写了代码,但它工作不正常
当我点击下一个按钮时它显示我下一个记录程序正常工作但是我退出并直接登录显示我最后一条记录不是从一开始..请回复
i have written the code but it is not working properly
when i click on next button it is showing me next record program works properly but whan I log out and log in directly shows me last record not from start..please reply
Question table colums are question_id,teacher_id,teacher_question,teacher_answer
//代码是
//code is
namespace project_master
{
public partial class acnj : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
public static int i = 0;
DataTable dt = new DataTable();
DataRow drr;
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False");
public void Page_Load(object sender, EventArgs e)
{
con.Open();
string s1 = "select student_id from Student where user_name='" + Session["sid"] + "'";
SqlCommand cmd = new SqlCommand(s1, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
Label4_stu_id.Text =dr[0].ToString();//for displaying student_id
dr.Close();
}
SqlDataAdapter da = new SqlDataAdapter("select * from Question where question_id>=1 and question_id<=3", con);
da.Fill(dt);
drr = dt.Rows[i];
Label2_tea_que.Text = "Write question below in Textbox click next to get questoion";
// i++;
}
public void next_Click(object sender, System.EventArgs e)
{
if (i == (dt.Rows.Count - 1))
{
Response.Write("Last record !");
}
else
{
i++;
}
Label3_que_id.Text = Convert.ToString(drr[0]);//this label for displaying question_id
Label2_tea_que.Text = Convert.ToString(drr[2]); //this label for displaying Teacher question
}
}
}
推荐答案
基本上,那里是这个问题的两种方法,您可以存储当前问题(当前i变量作为会话变量,例如
Basically, there are two approaches to this problem, you can store the current question (currently the i variable as a session variable, e.g.
Session["CurrentQuestion"]
,当用户退出时,请确保清理并放弃会话,如下所示:
, and when the user logs out, make sure to clean and abandon the session like this:
Session.Clear();
Session.Abandon();
否则,您可以使用IsPostback属性重置当前问题:
Otherwise, you can use the IsPostback property to reset the current question:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
i = 0;
}
...
}
这只会在第一次访问页面时重置当前问题,而不是在导航到下一个或上一个问题之后
This will only reset the current question on the first visit to the page, not after navigating to the next or previous question
这篇关于如何编写在线考试中的下一个按钮的代码以及前一个按钮的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!