【发布时间】:2016-05-03 12:27:58
【问题描述】:
在按钮的 onclick 事件中,我从 aspx 页面获取控件,并进一步使用 if 语句来查看哪个单选按钮处于活动状态并将相应的值存储在变量 selans ,使用这个selans,我会将它与隐藏字段的值进行比较,以确定选中的单选按钮是否为正确答案,如果答案正确,即selans中的值与隐藏中的值匹配字段(实际答案)和变量“count”(最初用值 0 初始化)相应地递增,并且所有这些代码都放在“for 循环”中,该循环将执行到第一个。 GridView 中的控件数量(您可以将其与问题编号相关联,因为 GridView 的每条记录都会生成新控件)。
我用过变量
1) totalgrid1:- 从gridview1获取总分
2)totalgrid2 :- 从 gridview2 获取总分
3)total :- totalgrid1 + totalgrid2...是考试总分
4)correct1 :- 从 gridview1 中获取正确答案的数量
5)correct2 :- 从 gridview2 中获取正确答案的数量
6)correct :- correct1 + correct2.....用户正确答案的总数
运行此程序后,即尝试以正确答案进行考试,我得到“分数 = 6”而不是在结果页面上得到 4,因为只有 4 个问题,每个问题都有 1 分,那么我怎样才能得到 6 分?和 我也得到“正确答案的数量=什么都没有显示(它是空白的)”......它应该显示一些价值。
我不知道我在哪里犯了错误。下面是我的代码。看看我的代码。告诉我我在哪里犯错以及解决方案是什么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
GridView1.DataBind();
GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
GridView2.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
RadioButton r1, r2, r3, r4;
HiddenField hdn;
int count = 0;
int neg = 0;
int total=0;
int totalgrid1=0;
int totalgrid2=0;
int attempt1 = 0;
int attempt2 = 0;
int Tattempt = 0;
int correct = 0;
int correct1 = 0;
int correct2 = 0;
string selans = "-1";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt1++;
if (hdn.Value == selans)
{
count++;
correct1++;
}
else
{
neg--;
}
}
totalgrid1 = count + neg;
}
for (int i = 0; i < GridView2.Rows.Count; i++)
{
r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = r1.Text;
}
else if (r2.Checked)
{
selans = r2.Text;
}
else if (r3.Checked)
{
selans = r3.Text;
}
else if (r4.Checked)
{
selans = r4.Text;
}
if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
{
attempt2++;
if (hdn.Value == selans)
{
count++;
correct2++;
}
else
{
neg--;
}
}
totalgrid2 = count + neg;
}
total = totalgrid1 + totalgrid2;
Tattempt = attempt1 + attempt2;
correct = correct1 + correct2;
Label2.Text = total.ToString();
Label3.Text = Tattempt.ToString();
Label4.Text = correct.ToString();
Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns" +Label4.Text);
}
}
Result.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Student_Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["Score"];
Label2.Text = Request.QueryString["AttemptedQues"];
Label3.Text = Request.QueryString["CorrectAns"];
}
}
【问题讨论】:
-
请提出具体问题。如果您希望有人检查您的代码,请发送电子邮件至Code Review。