Note that there are some explanatory texts on larger screens.

plurals
  1. POvalidate radiobuttonlist in a gridview using javascript
    text
    copied!<p>I am trying to validate that each radiobuttonlist has been selected. the radiobuttonlist is dynamically created from a sql database. The following line errors out "if (MyRadio[j].checked) {", the error is "checked' is null or not an object". The following is the code. Thank you</p> <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server"&gt; &lt;cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" CssClass="ajax__myTab" Width="100%" ScrollBars="Horizontal"&gt; &lt;cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="" Enabled="true"&gt; &lt;HeaderTemplate&gt;Main&lt;/HeaderTemplate&gt; &lt;ContentTemplate&gt; &lt;table cellpadding="3" cellspacing="1"&gt; &lt;tr&gt; &lt;td style="text-align: right"&gt;Audit Status:&lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID="ddlAuditStatus" runat="server"&gt; &lt;asp:ListItem&gt;InProgress&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Completed&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ContentTemplate&gt; &lt;/cc1:TabPanel&gt; &lt;cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="" Enabled="true"&gt; &lt;HeaderTemplate&gt;Questions&lt;/HeaderTemplate&gt; &lt;ContentTemplate&gt; &lt;asp:GridView ID="GridViewQuestions" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="5" CellSpacing="1" DataKeyNames="Pkey" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical"&gt; &lt;FooterStyle BackColor="#CCCCCC" /&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblQuestion" runat="server"&gt;&lt;/asp:Label&gt; &lt;asp:HiddenField ID="hdnPkey" runat="server" Value='&lt;%# Bind("Pkey") %&gt;' /&gt; &lt;asp:HiddenField ID="hdnRequiresAnAnswer" runat="server" Value='&lt;%# Bind("RequiresAnAnswer") %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Answer"&gt; &lt;ItemTemplate&gt; &lt;asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" Visible="false"&gt; &lt;asp:ListItem&gt;Yes&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;No&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;N/A&lt;/asp:ListItem&gt; &lt;/asp:RadioButtonList&gt; &lt;/ItemTemplate&gt; &lt;HeaderStyle Width="130px" /&gt; &lt;ItemStyle Width="130px" /&gt; &lt;/asp:TemplateField&gt; &lt;PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /&gt; &lt;SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /&gt; &lt;HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" /&gt; &lt;AlternatingRowStyle BackColor="#CCCCCC" /&gt; &lt;/asp:GridView&gt; &lt;/ContentTemplate&gt; &lt;/cc1:TabPanel&gt; &lt;/cc1:TabContainer&gt; &lt;script type="text/javascript"&gt; function fcnUpdateMain() { var MyStatus = document.getElementById("&lt;%=ddlAuditStatus.ClientID%&gt;").value var grid = document.getElementById("&lt;%= GridViewQuestions.ClientID %&gt;"); if (grid.rows.length &gt; 0) { for (i = 2; i &lt; grid.rows.length + 1; i++) { if (i &lt; 10) { i = "0" + i } else { i = i } var MyReqAnswer = document.getElementById("ctl00_ContentPlaceHolder2_TabContainer1_TabPanel2_GridViewQuestions_ctl" + i + "_hdnRequiresAnAnswer").value var MyRadio = document.getElementById("ctl00_ContentPlaceHolder2_TabContainer1_TabPanel2_GridViewQuestions_ctl" + i + "_RadioButtonList1") if (MyStatus == "Completed") { if (MyReqAnswer == "Yes") { var options = document.getElementsByTagName("input") for( x = 0; x &lt; options.length; ++x ) { if(options[x].type == "radio") { for (var j = 0; j &lt; MyRadio.length; j++) { if (MyRadio[j].checked) { } else { alert("You must select an answer for all questions.") document.getElementById("ctl00_ContentPlaceHolder2_TabContainer1_TabPanel2_GridViewQuestions_ctl" + i + "_ddlProductInterest").focus() return false; } } } } } } } } } &lt;/script&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload