Hi onais,
For there is no index at -1 you can set the Previous button visibile to false i.e. when you are in question 1 hide previous button.
For selecting the previous selected option you need to check the answer DataTable and get the saved value that user selected and set the RadioButton selection to true.
Check the below example.
HTML
<div id="dvQuestion" runat="server">
<h1>
<asp:Label ID="lblQuestion" runat="server" />
</h1>
<br />
<asp:RadioButtonList ID="rbtnOptions" runat="server">
</asp:RadioButtonList>
<br />
<br />
<asp:Button ID="btnPrevious" Text="Previous" runat="server" OnClick="Previous" />
<asp:Button ID="btnNext" Text="Next" runat="server" OnClick="Next" />
</div>
<div id="dvResult" runat="server" visible="false">
<h1>
<asp:Label ID="lblResult" runat="server" />
</h1>
<br />
<asp:Button ID="Button2" Text="Start Again" runat="server" OnClick="Start" />
</div>
<br />
<asp:GridView ID="gvAnswered" runat="server" />
C#
private DataTable DataTableQuestions
{
get { return (DataTable)ViewState["Question"]; }
set { ViewState["Question"] = value; }
}
private DataTable DataTableAnswered
{
get { return (DataTable)ViewState["Answer"]; }
set { ViewState["Answer"] = value; }
}
private int QuestionIndex
{
get { return (int)ViewState["QuestionIndex"]; }
set { ViewState["QuestionIndex"] = value; }
}
private int Score
{
get { return (int)ViewState["Score"]; }
set { ViewState["Score"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTableQuestions = PopulateQuestions();
QuestionIndex = 0;
Score = 0;
GetCurrentQuestion(QuestionIndex, DataTableQuestions);
if (QuestionIndex == 0)
{
btnPrevious.Visible = false;
}
}
}
protected void Next(object sender, EventArgs e)
{
QuestionIndex++;
GetCurrentQuestion(QuestionIndex, DataTableQuestions, int.Parse(rbtnOptions.SelectedItem.Value));
btnPrevious.Visible = true;
}
protected void Previous(object sender, EventArgs e)
{
QuestionIndex--;
GetCurrentQuestion(QuestionIndex, DataTableQuestions, int.Parse(rbtnOptions.SelectedItem.Value));
// Get the previous selected option.
string optionToSelect = DataTableAnswered.Rows[QuestionIndex]["AnswerGiven"].ToString();
// Set the RadioButtonList selected property to true by finding the selected Text.
rbtnOptions.Items.FindByText(DataTableQuestions.Rows[QuestionIndex][optionToSelect].ToString()).Selected = true;
if (QuestionIndex == 0)
{
btnPrevious.Visible = false;
}
else
{
btnPrevious.Visible = true;
}
}
protected void Start(object sender, EventArgs e)
{
Response.Redirect(Request.Url.AbsoluteUri);
}
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection _cn = new SqlConnection(constr))
{
using (SqlCommand _cmd = new SqlCommand("SELECT * FROM QuestionTable", _cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
{
_cn.Open();
da.Fill(dt);
_cn.Close();
}
}
}
return dt;
}
private void GetCurrentQuestion(int index, DataTable dtQuestions, int? selectedOption = null)
{
DataTable dtAnswered = new DataTable();
if (index > 0)
{
if (selectedOption != null)
{
string option = string.Empty;
DataRow row = dtQuestions.Rows[index - 1];
if (selectedOption == 0)
{
option = "OptionOne";
}
else if (selectedOption == 1)
{
option = "OptionTwo";
}
else if (selectedOption == 2)
{
option = "OptionThree";
}
else if (selectedOption == 3)
{
option = "OptionFour";
}
if (option == row["CorrectAnswer"].ToString())
{
Score++;
}
if (DataTableAnswered == null)
{
dtAnswered.Columns.AddRange(new DataColumn[3] { new DataColumn("QId", typeof(int)),
new DataColumn("Question", typeof(string)),
new DataColumn("AnswerGiven",typeof(string)) });
dtAnswered.Rows.Add(row["QuestionId"], row["QuestionDescription"], option);
DataTableAnswered = dtAnswered;
}
else
{
bool rowExist = DataTableAnswered.Select("QId=" + row["QuestionId"]).Count() > 0 ? true : false;
if (rowExist)
{
DataTableAnswered.Rows[QuestionIndex - 1]["AnswerGiven"] = option;
}
else
{
DataTableAnswered.Rows.Add(row["QuestionId"], row["QuestionDescription"], option);
}
}
}
}
if (index < dtQuestions.Rows.Count)
{
DataRow row = dtQuestions.Rows[index];
lblQuestion.Text = row["QuestionDescription"].ToString();
ListItem option1 = new ListItem(row["OptionOne"].ToString(), "0");
ListItem option2 = new ListItem(row["OptionTwo"].ToString(), "1");
ListItem option3 = new ListItem(row["OptionThree"].ToString(), "2");
ListItem option4 = new ListItem(row["OptionFour"].ToString(), "3");
rbtnOptions.Items.Clear();
rbtnOptions.Items.AddRange(new ListItem[4] { option1, option2, option3, option4 });
rbtnOptions.DataBind();
}
else
{
dvQuestion.Visible = false;
dvResult.Visible = true;
lblResult.Text = string.Format("You Scored {0}/{1}", Score, index);
// Code to save in Database.
gvAnswered.DataSource = DataTableAnswered;
gvAnswered.DataBind();
}
}
Screenshot
