2011-04-15 98 views
0

我正在開發一個使用ASP.NET(C#)& SQL Sever的在線考試系統項目。 這是我的代碼。在執行下一個&上一個按鈕的代碼時遇到問題。請給我答案。謝謝。如何編寫下一個和上一個按鈕的代碼?

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(); 
     } 
    } 
} 
+0

單擊「上一個」或「下一個」按鈕時,您預期會發生什麼? – Filburt 2011-04-15 07:07:03

回答

0

您可以從數據庫中提取隨機數據並將其存儲在數據集中。然後在那裏你可以用平常的方式得到問題。然後,您可以使用變量來獲取問題,該變量將存儲問題編號以獲取上一個問題和下一個問題。如果用戶點擊上一個,則變量 - 1如果下一個也爲+1

相關問題