how to validate radio button in gridview [Resolved]

Posted by Munder2013 under ASP.NET on 12/13/2013 | Points: 10 | Views : 8183 | Status : [Member] | Replies : 11
Hi Experts,
I'm facing problem to validate radio button in gridview.
I'm marking student attendance with single button and I placed 3 radio button rdbPresent, rdbAbsent & rdbLeave for each student.

I want alert on button click If I missed attendance for any Student.

Thanks & Regards
Baljeet Singh



Responses

Posted by: vishalneeraj-24503 on: 12/14/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Yes,you have to validate all your radio buttons in button click.
Because you all radio buttons are inside gridview.

write my above code inside button click as

protected boid btn_validate()
{
RadioButton rdbPresent = grid.rows[index].findcontrol("rdbPresent")
//find same for rest of the radiobuttons
//Here index is your gridview row index

//now check and validate
if(rdbPresent.checked == false && rdbAbsent.checked == false && rdbLeave.checked == false)
{
//message,please check either one radiobutton.
return;
}
}


Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
you can first find radio buttn inside gridview as
radiobutton rdbPresent = grid.rows[index].findcontrol("rdbPresent")
find same for rest of the radiobuttons

Here index is your gridview row index

now check

if(rdbPresent.checked==false &&rdbAbsent .checked==false && rdbLeave.checked == false)
{
//message,please check either one radiobuttons.
return;
}

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 12/14/2013 [Member] Starter | Points: 25

Up
0
Down
Will the entire code be placed on buttonClick event ???

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 12/14/2013 [Member] Starter | Points: 25

Up
0
Down
I have 3 radio buttons in one template field like this :

<asp:TemplateField HeaderText="Status">
<ItemTemplate>

<asp:RadioButton ID="rdbPresent" AutoPostBack="true" runat="server"
AccessKey="P" GroupName="s"
Text="Present" oncheckedchanged="rdbPresent_CheckedChanged" />
<asp:RadioButton ID="rdbAbsent" AutoPostBack="true" runat="server"
AccessKey="A" GroupName="s"
Text="Absent" oncheckedchanged="rdbAbsent_CheckedChanged" />
<asp:RadioButton ID="rdbLeave" AutoPostBack="true" runat="server" AccessKey="L" GroupName="s"
Text="Leave" oncheckedchanged="rdbLeave_CheckedChanged" />

</ItemTemplate>
</asp:TemplateField>

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/16/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Can you tell me you button is present inside the GirdView or outside of GridView?

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 12/16/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Allemahesh,

There is a single button outside of the GridView to mark attendance for all students.

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/16/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below javascript code:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>

<script type="text/javascript" language="javascript">

function ValidateSelection() {
var grid = document.getElementById("gv1");
var Selected = 0;
for (i = 1; i < grid.rows.length; i++) {
if (grid.rows[i].getElementsByTagName("input")[0] != null
&& grid.rows[i].getElementsByTagName("input")[0].type == "radio") {
if (grid.rows[i].getElementsByTagName("input")[0].checked == false
&& grid.rows[i].getElementsByTagName("input")[1].checked == false
&& grid.rows[i].getElementsByTagName("input")[2].checked == false) {
alert('Please select any row to proceed.');
return false;
}
}
}
return false;
}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:RadioButton ID="rdbPresent" runat="server" AccessKey="P" GroupName="s" Text="Present" />
<asp:RadioButton ID="rdbAbsent" runat="server" AccessKey="A" GroupName="s" Text="Absent" />
<asp:RadioButton ID="rdbLeave" runat="server" AccessKey="L" GroupName="s" Text="Leave" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ValidateSelection();" />
</button>
</div>
</form>
</body>
</html>


Happy Coding
If it helps you or directs U towards the solution,
MARK IT AS ANSWER


Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/16/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Did you try

protected boid btn_validate()
{
RadioButton rdbPresent = grid.rows[index].findcontrol("rdbPresent")
//find same for rest of the radiobuttons
//Here index is your gridview row index

//now check and validate
if(rdbPresent.checked == false && rdbAbsent.checked == false && rdbLeave.checked == false)
{
//message,please check either one radiobutton.
return;
}
}


Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 12/24/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Allemahesh,
Thanks for your response. I got solution

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 12/24/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Vishal,

Thank you Vishal. I placed that code inside foreach loop & its working fine

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Welcome Munder :)

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response