This way
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Timer: <asp:Label ID="lblTimer" runat="server" Text="60" ForeColor = "Red"></asp:Label><br />
<asp:Label ID="lblQuestion" runat="server" Text="Label"></asp:Label><br />
<asp:RadioButtonList ID="rblOptions" runat="server">
</asp:RadioButtonList>
<br />
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick = "OnNextClicked" />
<script type = "text/javascript">
var time = 120; //-- Timer of 120 seconds i.e. 2 minutes
window.onload = function () {
setInterval(function () {
var timer = document.getElementById("<%=lblTimer.ClientID %>");
time--;
timer.innerHTML = time;
if (time == 0) {
document.getElementById("<%=btnNext.ClientID %>").click();
}
}, 1000)
};
</script>
</form>
</body>
</html>
protected int QuestionNo
{
get
{
return Session["QuestionNo"] != null ? (int)Session["QuestionNo"] : 1;
}
set
{
Session["QuestionNo"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dataTable = new DataTable();
dataTable = GetData("SELECT [Q.NO],[QUESTION],[A],[B],[C],[D],[ANS] FROM ONLINETEST WHERE [Q.NO] = " + this.QuestionNo + "");
rblOptions.AppendDataBoundItems = true;
if (dataTable.Rows.Count > 0)
{
lblQuestion.Text = dataTable.Rows[0]["Q.NO"].ToString() + ". " + dataTable.Rows[0]["QUESTION"].ToString();
rblOptions.Items.Add(new ListItem { Text = "a." + dataTable.Rows[0]["A"].ToString(), Value = dataTable.Rows[0]["A"].ToString() });
rblOptions.Items.Add(new ListItem { Text = "b." + dataTable.Rows[0]["B"].ToString(), Value = dataTable.Rows[0]["B"].ToString() });
rblOptions.Items.Add(new ListItem { Text = "c." + dataTable.Rows[0]["C"].ToString(), Value = dataTable.Rows[0]["C"].ToString() });
rblOptions.Items.Add(new ListItem { Text = "d." + dataTable.Rows[0]["D"].ToString(), Value = dataTable.Rows[0]["D"].ToString() });
}
}
}
protected void OnNextClicked(object sender, EventArgs e)
{
//Here if you want you can write the code to save the answer selected by user in the database
this.QuestionNo++;
//Here you have to again redirect to the same page
Response.Redirect(Request.Url.AbsoluteUri);
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}