public partial class QuestionMaster : System.Web.UI.Page
{
int optionid;
DAl obj = new DAl();
int rec;
protected void Page_Load(object sender, EventArgs e)
{
string strgetsubid;
if (!IsPostBack)
{
strgetsubid = Session.Contents["subjectid"].ToString();
if (strgetsubid == null)
{
lblerror.Visible = true;
lblerror.Text = "Please select subject";
Response.Redirect("subjectlist.aspx");
lblerror.Visible = false;
}
else
{
obj.sqlstr = "select top 20 uniqid,Sno,question,option1,option2,option3,option4,answer,maxmark,explanation from questionmaster7 where f_subjectmaster7='" + strgetsubid + "'";
obj.dt = obj.getdata(obj.sqlstr);
obj.dt.Columns.Add("ans");
obj.dt.Columns.Add("bookmark");
foreach (DataRow dr in obj.dt.Rows)
{
dr["ans"] = 0;
dr["bookmark"] = 0;
}
Session["table"] = obj.dt;
showdata(0);
Session["recs"] = 0;
rec = 0;
}
}
else
{
obj.dt = (DataTable)Session["table"];
}
rec = Convert.ToInt32(Session["recs"]);
}
protected void btnmovefirst_Click(object sender, EventArgs e)
{
rec = 0;
Session["recs"] = rec;
showdata(rec);
}
void showdata(int recs)
{
try
{
lblqun .Text = Convert.ToString (recs + 1);
lblquen .Text = obj.dt.Rows[recs]["question"].ToString();
rdbtnoption1.Text = obj.dt.Rows[recs]["option1"].ToString();
rdbtnoption2.Text = obj.dt.Rows[recs]["option2"].ToString();
rdbtnoption3.Text = obj.dt.Rows[recs]["option3"].ToString();
rdbtnoptin4.Text = obj.dt.Rows[recs]["option4"].ToString();
try
{
optionid = Convert.ToInt32(obj.dt.Rows[recs]["ans"].ToString());
if (Convert.ToInt32(obj.dt.Rows[recs]["bookmark"].ToString()) == 1)
{
chkbox.Checked = true;
}
else
{
chkbox.Checked = false;
}
if (optionid == 1)
{
rdbtnoption1.Checked = true;
}
else if (optionid == 2)
{
rdbtnoption2.Checked = true;
}
else if (optionid == 3)
{
rdbtnoption3.Checked = true;
}
else if (optionid == 4)
{
rdbtnoptin4.Checked = true;
}
}
catch (Exception ex)
{
}
}
catch (Exception ex)
{
lblerror.Text = "not at position";
}
}
protected void btnmovelast_Click(object sender, EventArgs e)
{
rec = obj.dt.Rows.Count - 1;
Session["recs"] = rec;
showdata(rec);
}
public void saveans()
{
rec = Convert.ToInt32(Session["recs"]);
obj.dt = (DataTable)Session["table"];
obj.dt.Rows[rec]["ans"] = optionid;
if (chkbox.Checked == true)
{
obj.dt.Rows[rec]["bookmark"] = 1;
}
else
{
obj.dt.Rows[rec]["bookmark"] = 0;
}
setbuttoncolor(rec);
Session["table"] = obj.dt;
}
protected void btnmovenext_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = Convert.ToInt32(Session["recs"]);
rec = rec + 1;
Session["recs"] = rec;
showdata(rec);
}
public void setbuttoncolor(int id)
{
DataTable dt = (DataTable)Session["table"];
if (dt.Rows[id]["bookmark"].ToString() == "1" && Convert.ToInt32(dt.Rows[id]["ans"].ToString())!= 0)
{
setcolor (id,System .Drawing .Color .Blue );
}
if(dt.Rows [id]["bookmark"].ToString ()=="0" && Convert .ToInt32 (dt.Rows [id]["ans"].ToString ()) == 0)
{
setcolor (id,System .Drawing .Color .Red );
}
if (dt.Rows[id]["bookmark"].ToString ()=="1" && Convert .ToInt32 (dt.Rows [id]["ans"].ToString ()) == 0)
{
setcolor (id,System .Drawing .Color .Yellow );
}
if(dt.Rows [id]["bookmark"].ToString ()=="0" && Convert .ToInt32 (dt.Rows [id]["ans"].ToString ())!= 0)
{
setcolor (id,System .Drawing .Color .Green );
}
}
public void setcolor(int id, System.Drawing.Color color)
{
switch (id)
{
case 0: Lbtn1.BackColor = color;
break;
case 1: Lbtn2.BackColor = color;
break;
case 2: Lbtn3.BackColor = color;
break;
case 3: Lbtn4.BackColor = color;
break;
case 4: Lbtn5.BackColor = color;
break;
case 5: Lbtn6.BackColor = color;
break;
case 6: Lbtn7.BackColor = color;
break;
case 7: Lbtn8.BackColor = color;
break;
case 8: Lbtn9.BackColor = color;
break;
case 9: Lbtn10.BackColor = color;
break;
case 10: Lbtn11.BackColor = color;
break;
case 11: Lbtn12.BackColor = color;
break;
case 12: Lbtn13.BackColor = color;
break;
case 13: Lbtn14.BackColor = color;
break;
case 14: Lbtn15.BackColor = color;
break;
case 15: Lbtn16.BackColor = color;
break;
case 16: Lbtn17.BackColor = color;
break;
case 17: Lbtn18.BackColor = color;
break;
case 18: Lbtn19.BackColor = color;
break;
case 19: Lbtn20.BackColor = color;
break;
default:
break;
}
}
private void id(string qid)
{
obj.dt.Rows[rec]["ans"] = qid.ToString();
}
protected void btnmoveperious_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = Convert.ToInt32(Session["recs"]);
rec = rec - 1;
Session["recs"] = rec;
if (rec < 0)
{
return;
}
else
{
unselect();
showdata(rec);
}
}
public void unselect()
{
rdbtnoption1.Checked = false;
rdbtnoption2 .Checked =false ;
rdbtnoption3 .Checked =false ;
rdbtnoptin4 .Checked =false ;
}
protected void Lbtn2_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 1;
Session["recs"] = rec;
showdata(rec);
}
private void savecurrentvalue()
{
if (rdbtnoption1.Checked == true)
{
optionid = 1;
}
else if (rdbtnoption2.Checked == true)
{
optionid = 2;
}
else if (rdbtnoption3.Checked == true)
{
optionid = 3;
}
else if (rdbtnoptin4.Checked == true)
{
optionid = 4;
}
saveans();
unselect();
}
protected void Lbtn1_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 0;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn3_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 2;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn4_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 3;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn5_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 4;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn6_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 5;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn7_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 6;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn8_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 7;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn9_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 8;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn10_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 9;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn11_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 10;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn12_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 11;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn13_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 12;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn14_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 13;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn15_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 14;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn16_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 15;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn17_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 16;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn18_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 17;
Session["recs"] = rec;
showdata(rec);
}
protected void Lbtn19_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 18;
Session["recs"] = rec;
try
{
showdata(rec);
}
catch (Exception ex)
{
lblerror.Text = "Record is not found";
}
}
protected void Lbtn20_Click(object sender, EventArgs e)
{
savecurrentvalue();
rec = 19;
Session["recs"] = rec;
showdata(rec);
}
protected void btnExamend_Click(object sender, EventArgs e)
{
int totalmarks = 0;
foreach (DataRow dr in obj.dt.Rows)
{
if (Convert.ToInt32(dr["answer"]) == Convert.ToInt32(dr["ans"]))
{
totalmarks += 1;
}
}
lblMark .Text = totalmarks.ToString();
Session ["totalmarks"]=totalmarks;
Response .Redirect("Exam Result.aspx");
}
}