I have dropdowns inside gridiview so i want to validate if checkbox is checked and dropdown is not selected. The requirement is if you check any checkbox then you have to select something from the dropdownbox. here is my code
<script type="text/javascript">
function validateDDL() {
var flag = true;
var dropdowns = new Array(); //Create array to hold all the dropdown lists.
var gridview = document.getElementById('<%=gvSearch.ClientID%>'); //GridView1 is the id of ur gridview.
dropdowns = gridview.getElementsByTagName('Select'); //Get all dropdown lists contained in GridView1.
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns.item(i).value == 'Select') //If dropdown has no selected value
{
flag = false;
break; //break the loop as there is no need to check further.
}
}
if (!flag) {
dropdowns[i].focus();
alert('Please select a Project Role from the dropdown box. Thanks');
}
return flag;
}
</script>
gridview code
<asp:GridView ID="myGridview" runat="server"
AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
AllowPaging="true" ShowFooter="True"
OnPageIndexChanging="OnPaging">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="true">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("PRJ_ID")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" Visible="true">
<ItemTemplate>
<asp:Label ID="lblPrjTit" runat="server" Text='<%# Eval("PRJ_TITLE")%>'></asp:Label>
</ItemTemplate>
<%-- <ItemStyle Width="10px" />--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Role">
<ItemTemplate>
<asp:DropDownList ID="ddlRole" CssClass="form-control" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Check">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle Width="10px" />
</asp:TemplateField>
</Columns>
</asp:GridView>