Hi Awais,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>
<h1><asp:Label ID="lblQuestion" runat="server" /></h1>
</td>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="rbtnOptions" runat="server">
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnNext" Text="Next" runat="server" OnClick="OnNext" Style="float: left;" />
<asp:Button ID="btnPrevious" Text="Previous" runat="server" OnClick="OnPrevious" Style="float: right;" />
</td>
</tr>
</table>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
private DataTable QuestionsTable
{
get { return (DataTable)ViewState["Question"]; }
set { ViewState["Question"] = value; }
}
private DataTable AnsweredTable
{
get
{
if (ViewState["Answer"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Question", typeof(string));
dt.Columns.Add("Answered", typeof(int));
ViewState["Answer"] = dt;
return (DataTable)ViewState["Answer"];
}
else
{
return (DataTable)ViewState["Answer"];
}
}
set { ViewState["Answer"] = value; }
}
private int QuestionIndex
{
get { return (int)ViewState["QuestionIndex"]; }
set { ViewState["QuestionIndex"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
QuestionsTable = PopulateQuestions();
QuestionIndex = 0;
EnableDisableNextPrevious();
GetCurrentQuestion(QuestionIndex, QuestionsTable);
btnPrevious.Enabled = false;
}
}
protected void OnNext(object sender, EventArgs e)
{
SetSelectedAnswer();
QuestionIndex++;
EnableDisableNextPrevious();
GetCurrentQuestion(QuestionIndex, QuestionsTable);
SetSelectedOption();
}
protected void OnPrevious(object sender, EventArgs e)
{
SetSelectedAnswer();
QuestionIndex--;
EnableDisableNextPrevious();
GetCurrentQuestion(QuestionIndex, QuestionsTable);
SetSelectedOption();
}
private void SetSelectedAnswer()
{
DataTable dt = AnsweredTable;
DataRow[] drs = dt.Select("Question='" + lblQuestion.Text.Trim() + "'");
if (drs.Length > 0)
{
drs[0]["Answered"] = rbtnOptions.SelectedItem != null ? int.Parse(rbtnOptions.SelectedItem.Value) : (object)DBNull.Value;
}
else
{
DataRow dr = dt.NewRow();
dr["Question"] = lblQuestion.Text.Trim();
dr["Answered"] = rbtnOptions.SelectedItem != null ? int.Parse(rbtnOptions.SelectedItem.Value) : (object)DBNull.Value;
dt.Rows.Add(dr);
}
AnsweredTable = dt;
}
private void SetSelectedOption()
{
DataRow[] dr = AnsweredTable.Select("Question='" + lblQuestion.Text.Trim() + "'");
if (dr.Length > 0)
{
if (rbtnOptions.Items.FindByValue(dr[0]["Answered"].ToString()) != null)
{
rbtnOptions.Items.FindByValue(dr[0]["Answered"].ToString()).Selected = true;
}
}
}
private void EnableDisableNextPrevious()
{
btnPrevious.Enabled = QuestionIndex == 0 ? false : true;
btnNext.Enabled = QuestionIndex == QuestionsTable.Rows.Count - 1 ? false : true;
}
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
dt.Columns.Add("QuestionID", typeof(int));
dt.Columns.Add("QuestionStatement", typeof(string));
dt.Columns.Add("QuestionAnswer", typeof(string));
dt.Columns.Add("OptionA", typeof(string));
dt.Columns.Add("OptionB", typeof(string));
dt.Columns.Add("OptionC", typeof(string));
dt.Columns.Add("OptionD", typeof(string));
dt.Rows.Add(1, "What is an apple?", "Fruit", "Vegetable", "Hammond", "Fruit", "None");
dt.Rows.Add(2, "What is an carrot?", "Vege.", "VegeVegetable", "Hammond", "Fruit", "None");
dt.Rows.Add(3, "What is an egg?", "Chicken", "Vegetable", "Chicken", "Fruit", "None");
dt.Rows.Add(4, "What is an box?", "Wooden", "Vegetable", "Hammond", "Wooden", "None");
dt.Rows.Add(5, "What is an door?", "Steel", "Vegetable", "Hammond", "Fruit", "Steel");
dt.Rows.Add(6, "What is a piano?", "Musical Device", "Vegetable", "Hammond", "Fruit", "Musical Device");
return dt;
}
private void GetCurrentQuestion(int index, DataTable dtQuestions)
{
DataRow row = dtQuestions.Rows[index];
lblQuestion.Text = row["QuestionStatement"].ToString();
List<ListItem> options = new List<ListItem>();
options.AddRange(new ListItem[4] {
new ListItem(row["OptionA"].ToString(), "0"),
new ListItem(row["OptionB"].ToString(), "1"),
new ListItem(row["OptionC"].ToString(), "2"),
new ListItem(row["OptionD"].ToString(), "3")
});
rbtnOptions.Items.Clear();
rbtnOptions.Items.AddRange(options.ToArray());
rbtnOptions.DataBind();
}
VB.Net
Private Property QuestionsTable As DataTable
Get
Return CType(ViewState("Question"), DataTable)
End Get
Set(ByVal value As DataTable)
ViewState("Question") = value
End Set
End Property
Private Property AnsweredTable As DataTable
Get
If ViewState("Answer") Is Nothing Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Question", GetType(String))
dt.Columns.Add("Answered", GetType(Integer))
ViewState("Answer") = dt
Return CType(ViewState("Answer"), DataTable)
Else
Return CType(ViewState("Answer"), DataTable)
End If
End Get
Set(ByVal value As DataTable)
ViewState("Answer") = value
End Set
End Property
Private Property QuestionIndex As Integer
Get
Return CInt(ViewState("QuestionIndex"))
End Get
Set(ByVal value As Integer)
ViewState("QuestionIndex") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
QuestionsTable = PopulateQuestions()
QuestionIndex = 0
EnableDisableNextPrevious()
GetCurrentQuestion(QuestionIndex, QuestionsTable)
btnPrevious.Enabled = False
End If
End Sub
Protected Sub OnNext(ByVal sender As Object, ByVal e As EventArgs)
SetSelectedAnswer()
QuestionIndex += 1
EnableDisableNextPrevious()
GetCurrentQuestion(QuestionIndex, QuestionsTable)
SetSelectedOption()
End Sub
Protected Sub OnPrevious(ByVal sender As Object, ByVal e As EventArgs)
SetSelectedAnswer()
QuestionIndex -= 1
EnableDisableNextPrevious()
GetCurrentQuestion(QuestionIndex, QuestionsTable)
SetSelectedOption()
End Sub
Private Sub SetSelectedAnswer()
Dim dt As DataTable = AnsweredTable
Dim drs As DataRow() = dt.Select("Question='" & lblQuestion.Text.Trim() & "'")
If drs.Length > 0 Then
drs(0)("Answered") = If(rbtnOptions.SelectedItem IsNot Nothing, Integer.Parse(rbtnOptions.SelectedItem.Value), CObj(DBNull.Value))
Else
Dim dr As DataRow = dt.NewRow()
dr("Question") = lblQuestion.Text.Trim()
dr("Answered") = If(rbtnOptions.SelectedItem IsNot Nothing, Integer.Parse(rbtnOptions.SelectedItem.Value), CObj(DBNull.Value))
dt.Rows.Add(dr)
End If
AnsweredTable = dt
End Sub
Private Sub SetSelectedOption()
Dim dr As DataRow() = AnsweredTable.Select("Question='" & lblQuestion.Text.Trim() & "'")
If dr.Length > 0 Then
If rbtnOptions.Items.FindByValue(dr(0)("Answered").ToString()) IsNot Nothing Then
rbtnOptions.Items.FindByValue(dr(0)("Answered").ToString()).Selected = True
End If
End If
End Sub
Private Sub EnableDisableNextPrevious()
btnPrevious.Enabled = If(QuestionIndex = 0, False, True)
btnNext.Enabled = If(QuestionIndex = QuestionsTable.Rows.Count - 1, False, True)
End Sub
Private Function PopulateQuestions() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("QuestionID", GetType(Integer))
dt.Columns.Add("QuestionStatement", GetType(String))
dt.Columns.Add("QuestionAnswer", GetType(String))
dt.Columns.Add("OptionA", GetType(String))
dt.Columns.Add("OptionB", GetType(String))
dt.Columns.Add("OptionC", GetType(String))
dt.Columns.Add("OptionD", GetType(String))
dt.Rows.Add(1, "What is an apple?", "Fruit", "Vegetable", "Hammond", "Fruit", "None")
dt.Rows.Add(2, "What is an carrot?", "Vege.", "VegeVegetable", "Hammond", "Fruit", "None")
dt.Rows.Add(3, "What is an egg?", "Chicken", "Vegetable", "Chicken", "Fruit", "None")
dt.Rows.Add(4, "What is an box?", "Wooden", "Vegetable", "Hammond", "Wooden", "None")
dt.Rows.Add(5, "What is an door?", "Steel", "Vegetable", "Hammond", "Fruit", "Steel")
dt.Rows.Add(6, "What is a piano?", "Musical Device", "Vegetable", "Hammond", "Fruit", "Musical Device")
Return dt
End Function
Private Sub GetCurrentQuestion(ByVal index As Integer, ByVal dtQuestions As DataTable)
Dim row As DataRow = dtQuestions.Rows(index)
lblQuestion.Text = row("QuestionStatement").ToString()
Dim options As List(Of ListItem) = New List(Of ListItem)()
options.AddRange(New ListItem(3) {
New ListItem(row("OptionA").ToString(), "0"),
New ListItem(row("OptionB").ToString(), "1"),
New ListItem(row("OptionC").ToString(), "2"),
New ListItem(row("OptionD").ToString(), "3")})
rbtnOptions.Items.Clear()
rbtnOptions.Items.AddRange(options.ToArray())
rbtnOptions.DataBind()
End Sub
Screenshot