- count == 4 means for a peritcular question if OptionOne radioButton is not checked then i will increament the counter if any one of the RadioButton is checked then It will break that loop. One Question is having four option so it will be increamented till 4 if it will become four then this question is not attempted.
- i is the table index of a question index is started from 0 so i added 1 to it.
I have done some changes in the code if all the questions are attempted then it will not show you the alert message.
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js">
</script>
<script type="text/javascript">
function RadioCheck(rb) {
$(rb).parent().css("border", "4px solid #CCCCCC");
//finding table inside dlQuestion
var radioButton = $(rb).parent().parent().parent().parent().find("input");
var row = rb.parentNode.parentNode;
for (var i = 0; i < radioButton.length; i++) {
if (radioButton[i].type == "radio") {
if (radioButton[i] != rb) {
radioButton[i].checked = false;
$(radioButton[i]).parent().css("border", "0px");
}
}
}
};
</script>
<script type="text/javascript">
function ConfirmNotAttemptedQuestion() {
var count = 0;
var attempted = 0;
var notAttempted = "";
var table = $('[id*=DataList1]').find('table')
for (var i = 0; i < table.length; i++) {
var tb = $(table[i]);
var radioButton = tb.find('input');
for (var j = 0; j < radioButton.length; j++) {
if (radioButton[j].checked) {
attempted++;
count = 0;
break;
}
else {
count++;
}
if (count == 4) {
notAttempted = notAttempted + (i + 1) + ", ";
count = 0;
}
}
}
var message = notAttempted.substring(0, notAttempted.length - 2);
if (attempted < table.length) {
var b = confirm('Not Attempted Question' + message);
if (b == false) {
$("[id*=hdDemo]").val("false");event.preventDefault();
}
}
else {
$("[id*=hdDemo]").val("true");
}
}</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hdDemo" runat="server" />
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
(<%#Container.ItemIndex+1 %>)
<asp:Label ID="Label1" runat="server" Text='<%# bind("QuestionDescription") %>' Font-Bold="True"></asp:Label><br />
<table id="QuestionAnswer" runat="server">
<tr>
<td>
<asp:RadioButton ID="RadioButton1" onclick="RadioCheck(this);" runat="server" GroupName="Questions"
Text='<%# bind("OptionOne") %>' />
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton2" onclick="RadioCheck(this);" runat="server" GroupName="Questions"
Text='<%# bind("OptionTwo") %>' />
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton3" onclick="RadioCheck(this);" runat="server" GroupName="Questions"
Text='<%# bind("OptionThree") %>' />
</td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton4" onclick="RadioCheck(this);" runat="server" GroupName="Questions"
Text='<%# bind("OptionFour") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="ConfirmNotAttemptedQuestion();"
OnClick="Submit" />
</div>
</form>
</body>
</html>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populate();
}
}
private void Populate()
{
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection _cn = new SqlConnection(constr))
{
using (SqlCommand _cmd = new SqlCommand("SELECT * FROM QuestionTable", _cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
}
}
}
}
protected void Submit(object sender, EventArgs e)
{
if (this.hdDemo.Value == "true")
{
//Insert if user enter ok
int i = 0;
}
}
Thank You.