Please refer below code
HTML
<div>
<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="Button1" 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>
</div>
C#
private DataTable DataTableQuestions
{
get { return (DataTable)ViewState["Question"]; }
set { ViewState["Question"] = 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);
}
}
protected void Next(object sender, EventArgs e)
{
QuestionIndex++;
GetCurrentQuestion(QuestionIndex, DataTableQuestions, int.Parse(rbtnOptions.SelectedItem.Value));
}
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)
{
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 (index < dtQuestions.Rows.Count)
{
DataRow row = dtQuestions.Rows[index];
lblQuestion.Text = row["QuestionDescription"].ToString();
List<ListItem> options = new List<ListItem>();
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");
options.AddRange(new ListItem[4] { option1, option2, option3, option4 });
List<ListItem> randomOptions = RandomizeList(options);
rbtnOptions.Items.Clear();
rbtnOptions.Items.AddRange(randomOptions.ToArray());
rbtnOptions.DataBind();
}
else
{
dvQuestion.Visible = false;
dvResult.Visible = true;
lblResult.Text = string.Format("You Scored {0}/{1}", Score, index);
}
}
public List<ListItem> RandomizeList(List<ListItem> originalList)
{
List<ListItem> randomList = new List<ListItem>();
Random random = new Random();
ListItem value = default(ListItem);
while (originalList.Count() > 0)
{
var nextIndex = random.Next(0, originalList.Count());
value = originalList[nextIndex];
randomList.Add(value);
originalList.RemoveAt(nextIndex);
}
return randomList;
}
Screenshot