C# 如何为next&;编写代码;上一个按钮?
我正在使用ASP.NET(C#)和SQL Server开发一个在线考试系统项目。 这是我的密码。我在实现下一个和上一个按钮的代码时遇到问题。请告诉我答案。多谢各位C# 如何为next&;编写代码;上一个按钮?,c#,.net,asp.net,C#,.net,Asp.net,我正在使用ASP.NET(C#)和SQL Server开发一个在线考试系统项目。 这是我的密码。我在实现下一个和上一个按钮的代码时遇到问题。请告诉我答案。多谢各位 public partial class Default : Page { int count; string ans; int[] a=new int[5]; int t; int ctr; SqlConnection conn = new SqlConnection(Configu
public partial class Default : Page
{
int count;
string ans;
int[] a=new int[5];
int t;
int ctr;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
DateTime myDate;
DataTable dt = new DataTable();
DataRow dr;
public void Show()
{
Session["Answered"] = dt;
View v = this.View1;
Label l = default(Label);
l = (Label)v.FindControl("Label1");
l.Text = dt.Rows[ctr]["Serial"] + ".";
l = (Label)v.FindControl("Label2");
l.Text = dt.Rows[ctr]["question"].ToString();
RadioButtonList r = default(RadioButtonList);
r = (RadioButtonList)v.FindControl("RadioButtonList1");
r.Items.Clear();
r.Items.Add(dt.Rows[ctr]["choice1"].ToString());
r.Items.Add(dt.Rows[ctr]["choice2"].ToString());
r.Items.Add(dt.Rows[ctr]["choice3"].ToString());
r.Items.Add(dt.Rows[ctr]["choice4"].ToString());
r.SelectedIndex = Convert.ToInt32(dt.Rows[ctr]["selected"]);
Session["ctr"] = ctr;
}
protected void Timer1_Tick(object sender, System.EventArgs e)
{
DateTime mydate2 = DateTime.Now;
DateTime mydate3 = default(DateTime);
try
{
mydate3 = Convert.ToDateTime((myDate - mydate2).ToString());
this.Label5.Text = "Time Left: " + mydate3.ToShortTimeString();
}
catch (Exception ex)
{
this.Label5.Text = "Error Setting up the Timer. Contact Admin";
}
if (mydate3.ToShortTimeString() == "00:00:00")
{
int marks = 0;
Session["Answered"] = dt;
Response.Redirect("default3.aspx?marks=" + marks);
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
DateTime myDate = new DateTime();
myDate =Convert.ToDateTime(Request.Cookies["start"].Value);
if (!IsPostBack) {
this.MultiView1.ActiveViewIndex = 0;
conn.Open();
cmd.Connection = conn;
Random arbit = new Random();
for (int i = 0; i <= a.GetUpperBound(0); i++) {
t = arbit.Next(1, 10);
if (Array.IndexOf(a, t) == -1) {
a[i] = t;
} else {
goto X;
}
}
for (int i = 0; i <= 4; i++)
{
cmd.CommandText = "select * from test where Serial=" + a[i];
da.SelectCommand = cmd;
da.Fill(ds, "test");
}
conn.Close();
dt = new DataTable("Answered");
dt.Columns.Add("Serial", typeof(int));
dt.Columns.Add("question", typeof(string));
dt.Columns.Add("choice1", typeof(string));
dt.Columns.Add("choice2", typeof(string));
dt.Columns.Add("choice3", typeof(string));
dt.Columns.Add("choice4", typeof(string));
dt.Columns.Add("correct", typeof(string));
dt.Columns.Add("selected", typeof(int));
DataRow r = null;
foreach (DataRow r_loopVariable in ds.Tables["test"].Rows) {
r = r_loopVariable;
dr = dt.NewRow();
dr["Serial"] = dt.Rows.Count + 1;
dr["question"] = r["question"];
dr["choice1"] = r["choice1"];
dr["choice2"] = r["choice2"];
dr["choice3"] = r["choice3"];
dr["choice4"] = r["choice4"];
dr["correct"] = r["correct"];
dr["selected"] = -1;
dt.Rows.Add(dr);
}
Session["Answered"] = dt;
Show();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
Session["ctr"] = ctr;
Session["Answered"] = dt;
Session["ctr"] = ctr;
ctr += 1;
Show();
if (ctr == 4)
{
this.btnNext.Enabled = false;
}
this.btnPrev.Enabled = true;
}
protected void btnPrev_Click(object sender, EventArgs e)
{
Session["ctr"] = ctr;
Session["Answered"] = dt;
ctr = ctr - 1;
if (ctr == 0)
{
this.btnPrev.Enabled = false;
}
Session["ctr"] = ctr;
this.btnNext.Enabled = true;
Show();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList1.SelectedIndexChanged += new EventHandler(RadioButtonList1_SelectedIndexChanged);
Session["Answered"] = dt;
}
protected void btnShowMarks_Click(object sender, EventArgs e)
{
int marks = 0;
Session["Answered"] = dt;
Response.Redirect("default3.aspx?marks=" + marks);
Session["marks"] = dt;
int []b=new int[6];
foreach (int c in b)
{
RadioButtonList1.SelectedIndex.ToString();
}
}
}
公共部分类默认值:第页
{
整数计数;
字符串ans;
int[]a=新的int[5];
int t;
国际中心;
SqlConnection conn=新的SqlConnection(ConfigurationManager.AppSettings[“ConnectionString”].ToString());
SqlDataAdapter da=新的SqlDataAdapter();
SqlCommand cmd=新的SqlCommand();
数据集ds=新数据集();
日期时间myDate;
DataTable dt=新的DataTable();
数据行dr;
公开展览(
{
会话[“已回答”]=dt;
视图v=此。视图1;
标签l=默认值(标签);
l=(标签)v.FindControl(“标签1”);
l、 Text=dt.行[ctr][“串行”]+”;
l=(标签)v.FindControl(“标签2”);
l、 Text=dt.Rows[ctr][“问题”].ToString();
RadioButtonList r=默认值(RadioButtonList);
r=(RadioButtonList)v.FindControl(“RadioButtonList 1”);
r、 Items.Clear();
r、 Items.Add(dt.Rows[ctr][“choice1”].ToString());
r、 Items.Add(dt.Rows[ctr][“choice2”].ToString());
r、 Items.Add(dt.Rows[ctr][“choice3”].ToString());
r、 Items.Add(dt.Rows[ctr][“choice4”].ToString());
r、 SelectedIndex=Convert.ToInt32(dt.Rows[ctr][“selected”]);
会话[“ctr”]=ctr;
}
受保护的无效计时器1_刻度(对象发送方,System.EventArgs e)
{
DateTime mydate2=DateTime.Now;
DateTime mydate3=默认值(DateTime);
尝试
{
mydate3=Convert.ToDateTime((myDate-mydate2.ToString());
this.Label5.Text=“剩余时间:”+mydate3.ToSortTimeString();
}
捕获(例外情况除外)
{
this.Label5.Text=“设置计时器时出错。请与管理员联系”;
}
如果(mydate3.ToSortTimeString()=“00:00:00”)
{
整数分=0;
会话[“已回答”]=dt;
响应.重定向(“default3.aspx?marks=“+marks”);
}
}
受保护的无效页面加载(对象发送方,System.EventArgs e)
{
DateTime myDate=新的日期时间();
myDate=Convert.ToDateTime(Request.Cookies[“start”].Value);
如果(!IsPostBack){
this.MultiView1.ActiveViewIndex=0;
conn.Open();
cmd.Connection=conn;
Random arbit=新的Random();
对于(int i=0;i我建议您使用向导控件。
以下是一些例子:
我建议您使用向导控件。
以下是一些例子:
< p>因为你似乎开发了类似于巫师的形式,你考虑使用.因为你似乎开发了一个类似巫师的形式,你考虑使用.< /p> < p>你可以从数据库中获取随机数据并把它存储在一个数据集中。然后你可以用通常的方式得到问题。然后你可以用一个变量来存储问题,这些变量将存储Q。获取上一个和下一个问题的问题编号。如果用户单击上一个,则变量-1也+1如果下一个,则可以从数据库中获取随机数据并将其存储在数据集中。然后,您可以以常规方式获取问题。然后,您可以使用存储问题编号的变量获取问题,以获取上一个和下一个问题。If用户单击“上一步”或“下一步”按钮时,您实际预计会发生什么?单击“上一步”或“下一步”按钮时,您实际预计会发生什么?