Online Test

Self-Innovator
Posted by Self-Innovator under ASP.NET category on | Points: 40 | Views : 6392
The below code snippet provides code for Online Test
Page Design
<form id="form1" runat="server"> <div align="center">
<asp:Label ID="lblHeading" runat="server" Text="Online Test" Font-Bold="true"></asp:Label>
</div><div>
<asp:Label ID="lblTimeLeft" runat="server" Text="Time Left" Font-Bold="True"></asp:Label>
</div> <div align="right">
<asp:Label ID="lblID" runat="server" Text="Candidate Id"></asp:Label><br />
<asp:Label ID="lblTimeStart" runat="server" Font-Bold="True">Exam started on</asp:Label>
</div><table> <tr><td>
<asp:Label ID="lblQstn1" runat="server"></asp:Label>
</td></tr><tr><td> <asp:RadioButtonList ID="rblAnswrs" runat="server"></asp:RadioButtonList>
</td></tr></table>
</form>

Code Behind (.cs)
  //Global Var Declaration 
long timerStartValue = 1800;
string Conn = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
Page Load
protected void Page_Load(object sender, EventArgs e)
{ try
{ System.Configuration.Configuration web = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
web.AppSettings.Settings.Remove("Delay");
web.AppSettings.Settings.Add("Delay", "" + 300 * 1000 + "");
web.Save(ConfigurationSaveMode.Modified); }
catch (Exception ex)
{ Response.Write("<script>alert(''" + ex.Message + "'')</script>"); }
if (!Page.IsPostBack)
{ lblTimeStart.Text +=" "+DateTime.Now.ToShortTimeString();
this.timerStartValue = long.Parse(ConfigurationManager.AppSettings["Delay"].ToString());
this.TimerInterval = 500; try {
SqlConnection cnn = new SqlConnection(Conn);
SqlCommand cmdQ = new SqlCommand("select * from tbl_Qstns", cnn);
SqlCommand cmdA = new SqlCommand("select * from tbl_Answrs", cnn);
SqlDataAdapter adaQ = new SqlDataAdapter(cmdQ);
SqlDataAdapter adaA = new SqlDataAdapter(cmdA);
DataSet dsQstn = new DataSet();DataSet dsAns = new DataSet();
adaQ.Fill(dsQstn); adaA.Fill(dsAns);
if (dsQstn.Tables[0].Rows.Count > 0) {
lblQstn1.Text = dsQstn.Tables[0].Rows[0]["Questions"].ToString(); }
if (dsAns.Tables[0].Rows.Count > 0) {
rblAnswrs.DataSource = dsAns;
rblAnswrs.DataTextField = "Answers";
rblAnswrs.DataValueField = "id";
rblAnswrs.DataBind();
} }
catch (Exception ex) {
Response.Write("<script>alert(''" + ex.Message + "'')");
} } }
Page PreRender Event
void Page_PreRender(object sender, EventArgs e)
{
StringBuilder bldr = new StringBuilder();
bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lblTimeLeft.ClientID);
bldr.Append("Timer.go()");
ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}
Page Pre Initialization Event
void Page_PreInit(object sender, EventArgs e)
{ string timerVal = Request.Form["timerData"];
if (timerVal != null || timerVal == "")
{ timerVal = timerVal.Replace(",", String.Empty);
timerStartValue = long.Parse(timerVal);
} }
User Defined Methhod
private Int32 TimerInterval
{ get
{ object o = ViewState["timerInterval"];
if (o != null) { return Int32.Parse(o.ToString()); }
return 50;
} set { ViewState["timerInterval"] = value; }
}

Javascript Function for Calculating Test Time
 <script type="text/javascript">
function myTimer(startVal,interval,outputId, dataField){
this.value = startVal;
this.OutputCntrl = document.getElementById(outputId);
this.currentTimeOut = null;
this.interval = interval;
this.stopped=false;
this.data = null;
var formEls = document.form1.elements;
if(dataField)
{ for(var i=0; i < formEls.length -1; i++)
{ if(formEls[i].name == dataField)
{ this.data = formEls[i];
i = formEls.length + 1;
} } }
myTimer.prototype.go = function ()
{ if(this.value > 0 && this.stopped == false)
{ this.value = (this.value - this.interval);
if(this.data)
{ this.data.value = this.value; }
var current = this.value;
this.OutputCntrl.innerHTML = this.Hours(current) + ':' + this.Minutes(current) + ':' + this.Seconds(current);
this.currentTimeOut = setTimeout("Timer.go()",this.interval); }
else
{ alert('Time Out!'); window.location('Home.aspx'); }
}
myTimer.prototype.stop = function (){
this.stopped = true; if(this.currentTimeOut != null)
{ clearTimeout(this.currentTimeout); } }
myTimer.prototype.Hours = function(value){
return Math.floor(value/3600000); //Indicates 5 Minutes of Test can modify according to the requirements
}
myTimer.prototype.Minutes = function(value){
return Math.floor((value - (this.Hours(value)*3600000))/60000);
}
myTimer.prototype.Seconds = function(value){
var hoursMillSecs = (this.Hours(value)*3600000)
var minutesMillSecs = (this.Minutes(value)*60000)
var total = (hoursMillSecs + minutesMillSecs)
var ans = Math.floor(((this.value - total)%60000)/1000);
if(ans < 10)
return "0" + ans; return ans;
} }
</script>

Comments or Responses

Login to post response