Gridview Validations:
Description:
If the gridview is having Checkbox, Dropdown, Textbos in the templeate columns,
then below are the validations for Gridview using javascript
The validations are in the following order.
1. At least one check box to be checked.
2. If the checkbox is checked, enter text box value 3. If the textbos is filled, select item from drodown
<script type="text/javascript"> function ValidateGridview() { var delstart = "false"; if(document.getElementById("GridView1")) { dgs = document.getElementById("GridView1"); var idSW=2; for(var i=1;i { if(dgs.rows[i]) { if(parseInt(idSW,10) <= 9) { idSW = "0" + idSW; } var chkCheckBox = "GridView1_ctl" + idSW + "_Chk"; if(document.getElementById(chkCheckBox).checked == true) { delstart="true"; break; } } idSW++; } } // checks if the checkbox is checked or not... if(delstart=="false") { alert('Please select at least one product'); return false; } if(document.getElementById("GridView1")) { dgs = document.getElementById("GridView1"); var id=2; for(var i=1;i { if(dgs.rows[i]) { if(parseInt(id,10) <= 9) { id = "0" + id; } var chkCheckBox = "GridView1_ctl" + id + "_Chk"; //Checking whether checkbox is checked or not. if(document.getElementById(chkCheckBox).checked == true) { var txText = "GridView1_ctl" + id + "_txt"; //Checking whether textbox is empty or not. if(document.getElementById(txText).value == "") { alert('Please enter no of licenses.'); document.getElementById(txText).focus(); return false; } else if(document.getElementById(txText).value != "") { //Checks if the textbox value is zero or not and if it is zero gives message if(parseInt(document.getElementById(txText).value,10) == 0) { alert('value should be greater than zero.'); document.getElementById(txText).focus(); return false; } //Comparing Gridview binded value with textbox value.. if(parseInt(dgs.rows[i].cells[2].innerText,10) < parseInt(document.getElementById(txText).value,10)) { alert('value cannot be greater than no of available licenses entered.'); document.getElementById(txText).focus(); return false; }
var DropDown = "GridView1_ctl" + id + "_ddl"; if(document.getElementById(DropDown).selectedIndex <= 0) { alert('Please select the items from the list'); document.getElementById(DropDown).focus(); return false; } } } id++; } } } else return true; } </script>
Design View:
<asp:GridView ID="GridViewSoftware" DataKeyNames="ProductId" runat="server" BackColor="Window" AutoGenerateColumns="False" Width="95%"> <RowStyle CssClass="GridRowStyle" /> <HeaderStyle CssClass="FixedGridHeader" HorizontalAlign="Center" /> <AlternatingRowStyle CssClass="GridAlternateRowStyle" /> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="Ch" runat="server" /> </ItemTemplate> </asp:TemplateField>
<asp:BoundField DataField="Product" HeaderText="Product"></asp:BoundField> <asp:BoundField DataField="Total" HeaderText="Total Licences"></asp:BoundField>
<asp:TemplateField HeaderText="Required Licences"> <ItemTemplate> <asp:TextBox ID="txt" Runat="server" CssClass="Text" Width="70" onkeypress="return EnableKeys(2)" > </asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Select Product"> <ItemTemplate> <asp:DropdownList ID="ddl" Runat="server" CssClass="Text" Width="70"" > </asp:DropdownList> </ItemTemplate> </asp:TemplateField>
</Columns> </asp:GridView>
|
| Author: Miss Meetu Choudhary 27 Jul 2009 | Member Level: Diamond Points : 1 |
please use proper tag (close it with ;) to display proper angular brackets.
++ Thanks and Regards. Meetu Choudhary. Site Coordinator.
|