How To Find If Any Radio Button Selected In A Dynamic List Within A Gridview

How to find out if any radio button selected within a grdiview or not, and the radio button is getting generated dyanmically, in that case how could you validate easliy, i am sharing the simple jquery solution.

Suppose you are creating an application using Asp.Net gridview, and you having a demand in your application to create dynamic radio button list, for example suppose you are creating a online test application where you have 4 optional answer of a question, and we are binding these questions and answer from our database using sqlserver and binding them in Gridview. So basically we don't know how many radio button are there? In the below code i will explain how we can do this easily with Jquery.
 
Aspx Code 
  1. asp:GridView ID="grdquestions" runat="server" AutoGenerateColumns="false" OnRowDataBound="grdquestions_RowDataBound" DataKeyNames="QuestionId" Width="100%">  
  2. <Columns>  
  3.     <asp:TemplateField HeaderText="Asp .Net Online Test">  
  4.         <ItemTemplate>  
  5.             <table class="tableclass" id='<%#Eval("QuestionId") %>'>  
  6.                 <tr>  
  7.                     <td>  
  8.                         <b>Question <%#Eval("QuestionId") %> -: <%#Eval("Question") %>  
  9.                         </b>  
  10.                         <asp:HiddenField ID="hdnquestionId" runat="server"/>  
  11.                     </td>  
  12.                 </tr>  
  13.                 <tr>  
  14.                     <td>  
  15.                         <table>  
  16.                             <tr>  
  17.                                 <td>  
  18.                                     <table class="tblOptions">  
  19.                                         <tr>  
  20.                                             <td>  
  21.                                                 <asp:RadioButton ID="rdOption1" runat="server" Text='<%#Eval("Option1") %>' GroupName='  
  22.                                                     <%#Eval("QuestionId") %>/>  
  23.                                                 </td>  
  24.                                             </tr>  
  25.                                             <tr>  
  26.                                                 <td>  
  27.                                                     <asp:RadioButton ID="rdOption2" runat="server" Text='<%#Eval("Option2") %>' GroupName='  
  28.                                                         <%#Eval("QuestionId") %>/>  
  29.                                                     </td>  
  30.                                                 </tr>  
  31.                                                 <tr>  
  32.                                                     <td>  
  33.                                                         <asp:RadioButton ID="rdOption3" runat="server" Text='<%#Eval("Option3") %>' GroupName='  
  34.                                                             <%#Eval("QuestionId") %>/>  
  35.                                                         </td>  
  36.                                                     </tr>  
  37.                                                     <tr>  
  38.                                                         <td>  
  39.                                                             <asp:RadioButton ID="rdOption4" runat="server" Text='<%#Eval("Option4") %>' GroupName='  
  40.                                                                 <%#Eval("QuestionId") %>/>  
  41.                                                             </td>  
  42.                                                         </tr>  
  43.                                                         <tr>  
  44.                                                             <td>  
  45.                                                                 <asp:Label ID="lbquestionstatus" runat="server"></asp:Label>  
  46.                                                             </td>  
  47.                                                         </tr>  
  48.                                                         <tr>  
  49.                                                             <td class="correctAnswer">Correct Answer Is -:  
  50.   
  51.                                                                 <asp:Label ID="lbAnswer" runat="server" Text='<%#Eval("QuestionAnswer") %>'>  
  52.                                                                 </asp:Label>  
  53.                                                             </td>  
  54.                                                         </tr>  
  55.                                                     </table>  
  56.                                                 </td>  
  57.                                             </tr>  
  58.                                         </table>  
  59.                                     </td>  
  60.                                 </tr>  
  61.                             </table>  
  62.                         </ItemTemplate>  
  63.                     </asp:TemplateField>  
  64.                 </Columns>  
  65.             </asp:GridView>  
This code will generate the output like this,

 
Now how we will check while the user will click on submit button if any radio button is selected within a question or not? We have many ways to validate this, we can validate it on server side on button click or we can validate it on client side. I am here explaining on client side.
 
Css
  1. <style>
    .border {
          border-color: red;
          border-width: 2px;
          border-style: solid;
          width: 100%;
    }
     
    .tblOptions {
        width: 100%;
    }
     </style>
Script
  1.   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
  2. <script> 
    function CheckIfAllQuestionAnswerHasBeenSubmitted() {
           var numItems = $('.tblOptions').length;
           var flagtocheckcount = 0;
           $(".tblOptions").each(function () {
           var groupname = $('input[type=radio]:first', this).attr('name');
           if (!$("input[name='" + groupname + "']:checked").val()) {
           $(this).parents(".tableclass").addClass("border");
           }
           else {
                 flagtocheckcount = flagtocheckcount + 1;
               }
         });
       }
    </script> 
In the above mentioned Jquery code, we have done the validation for the image showing above. We created a small jquery function which we calling at the button click, and this function will check if any question answer option is not selected (radio button) then this function will add a class in outer table of that question, and class will highlight the border of that question, so that user can easliy understand that he or she don't selected the answer of that question.

If you see the jquery code you will see that we have done nothing extraordinary, we have a class(tbloptions) in outer table, which will be on every dynamic table that will generate in Gridview. We using a foreach loop on this class because we know this class will be there for each table, and then finding radio button withing that table which having this class, finding radio button with the type specifier of that element and getting the name of group from the first element of radio button.

And that's it when we have group name we can easily find out if any button is selected in the group or not.You can find complete code with in the code snippet, i have posted there with zip file.
Ebook Download
View all
Learn
View all