0

I have one grid-view inside grid-view using one RadioButtonList and outside grid-view using one asp.net Button when user click that button all radio button list should be selected, then only that Button event should be submitted.

Below is my grid-view RadioButtonList.

<asp:TemplateField HeaderText="Add Score">
<ItemTemplate>

<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="cssrbt">
<asp:ListItem Text="Correct" Value="1"></asp:ListItem>
<asp:ListItem Text="Wrong" Value="2"></asp:ListItem>
</asp:RadioButtonList>

</ItemTemplate>
</asp:TemplateField>

Below is my button which using out side of gridview.

<asp:Button ID="btn_Submit" runat="server" class="btn btn-md btn-danger" Text="Submit" OnClick="btn_Submit_Click"  ValidationGroup="viva" />

My issue is without selection inserting all grid-view data and that I want to validate.

Julien Marrec
  • 11,605
  • 4
  • 46
  • 63
MMK
  • 109
  • 9
  • Please this question is unclear. You has tagged it a `javascript` / `jquery`and by now, there is no javascript issues. – McNets Nov 16 '16 at 08:34
  • Hello I am using below answer java script, can you help for this solution, please check below answer. @mcNets – MMK Nov 16 '16 at 08:56

2 Answers2

0

You can bind javascript client event to button and iterate each radiobuttonlist to validate if atleast on radio button is selected.

$('<%= btn_Submit.ClientID %>').click(function(){
  $('.cssrbt').each(function(){
    if($(this).find(':radio:selected').length == 0)
         console.log("One of radio button list option must be selected")
  });
});
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Wait I am cheeking. @Adil – MMK Nov 16 '16 at 07:54
  • I am not getting any message and directly taking submission without validation same issue and I am using content page of master page. @Adil – MMK Nov 16 '16 at 08:00
  • I am getting this error: Uncaught ReferenceError: $ is not defined(…). @Adil – MMK Nov 16 '16 at 08:07
  • Include latest jQuery in your page – Adil Nov 16 '16 at 08:55
  • I added this script then also nothing alert is coming: . @Adil – MMK Nov 16 '16 at 09:18
  • console.log("One of radio button list option must be selected") will right in console use alert("invalid"); also debug javascript code to know what is happening, http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code – Adil Nov 16 '16 at 09:39
  • Now I am getting this error: Failed to load resource: the server responded with a status of 404 (Not Found) and press f12 and checked. @Adil – MMK Nov 16 '16 at 09:42
  • What resource? debug and find out! also check if you can download the jQuery file from the link you added. – Adil Nov 16 '16 at 09:44
  • Can you provide one script and I resolve resource error, nothing is coming, java script button click only not firing. @Adil – MMK Nov 16 '16 at 09:57
  • where did you put this javascript code? add in document.ready event also make sure document.ready is fired, https://learn.jquery.com/using-jquery-core/document-ready/ – Adil Nov 16 '16 at 09:59
  • . @Adil – MMK Nov 16 '16 at 10:06
  • see above is my code one more think I want to tell gridview and button should be inside in update panel?. @Adil – MMK Nov 16 '16 at 10:08
0

The example that adil gave needs JQuery library to implement and if you dont want to use any library only javascript you can validate like this

 <form runat="server" id="form1" action="Test.aspx.cs">

    <asp:TemplateField HeaderText="Add Score">
<ItemTemplate>

<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="cssrbt">
<asp:ListItem Text="Correct" Value="1"></asp:ListItem>
<asp:ListItem Text="Wrong" Value="2"></asp:ListItem>
</asp:RadioButtonList>


</ItemTemplate>
</asp:TemplateField>


          <asp:Button ID="btn_Submit" runat="server" class="btn btn-md btn-danger" Text="Submit" OnClientClick="return Validate('RadioButtonList1')" OnClick="btn_Submit_OnClick"  />

    </form>


    <script language="javascript" type="text/javascript">
                function Validate(id) {
                    var radiobutton = document.getElementsByName(id);
                    var flag;
                    for (var j = 0; j < radiobutton.length; j++) {
                        if (radiobutton[j].checked !== true) {
                            flag = true;
                        } else {
                            break;
                        }


                    }

                    if (flag) {
                        alert("Please select one option");
                        return false;

                    } 
                }
            </script>
Usman
  • 4,615
  • 2
  • 17
  • 33
  • I told you in above question button is outside of gridview not inside of gridview. @Usman – MMK Nov 16 '16 at 09:20
  • i did update the answer and button is now outside grid view as you can see in code – Usman Nov 16 '16 at 09:48
  • I already tried but not working event alert also not coming. @Usman – MMK Nov 16 '16 at 09:50
  • Not error is showing and one more think I want to tell gridview and button should be inside in update panel? becuase page is loading. @Usman – MMK Nov 16 '16 at 10:11