In my web application (asp.net & vb code), I encountered an issue involving two different gridviews. Each gridview had checkboxes to select records for batch update, along with corresponding buttons for batch update actions.
A validation was required to ensure that users had checked checkboxes before clicking the corresponding batch update button.
My code was working fine and able to display an error message if no checkbox was checked before clicking the batch update button. However, I discovered an error in the functionality. For example, when I checked a checkbox in gridview2 and then clicked the batch update button for gridview1, no error message was shown. It seems that my JavaScript was unable to identify the checkbox associated with another gridview.
Here is my JavaScript code:
function IsSelectedAtleastOneOT() {
var loTable1 = document.all("<%=Gridview1.ClientID%>"); // GridView Name
count1 = 0;
with (document.forms[0]) {
for (var i = 0; i < elements.length; i++) {
var e1 = elements[i];
e.id.substring(e.id.lastIndexOf('_') + 1, e.id.length) == 'ControlName') // This is our control Name
if (e1.type == "checkbox" && e1.checked == true) // This is our control Name
{
count1 += 1;
}
}
}
if (count1 == 0) {
alert("You have not selected any record.");
return false;
}
return true;
}
function IsSelectedAtleastOneActualDur() {
var loTable2 = document.all("<%=Gridview2.ClientID%>"); // GridView Name
count2 = 0;
with (document.forms[0]) {
for (var i = 0; i < elements.length; i++) {
var e2 = elements[i];
if (e2.type == "checkbox" && e2.checked == true )
{
count2 += 1;
}
}
}
if (count2 == 0) {
alert("You have not select any record.");
return false;
}
return true;
}
and the ASP.net code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize ="10" AutoGenerateColumns="False"
CellSpacing="1" DataSourceID="SqlDataSource1" CellPadding="2"
AllowSorting="True" DataKeyNames="ot_key" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="action,ot_key" DataNavigateUrlFormatString="{0}?id={1}"
DataTextField="post_code" HeaderText="Staff" SortExpression="post_code" meta:resourcekey="GridView1_post_code"/>
.......
</Columns>
<EmptyDataTemplate>
<span style="font-size: 14pt; color: #990000">
<asp:Localize runat="server" ID="text_no_record" Text="No records found." meta:resourcekey="text_no_record" />
</span>
</EmptyDataTemplate>
</asp:GridView>
<asp:Button ID="btnBatchUpdate" runat="server" Text="Batch Recommend/Approve Overtime Work"
OnClick="btnBatchUpdate_Click"
OnClientClick="return IsSelectedAtleastOneOT();" Visible="false"
Width="277px" meta:resourcekey="btnBatchUpdate" />
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellSpacing="1" DataSourceID="SqlDataSource3" CellPadding="2" AllowSorting="True" DataKeyNames="actual_dur_key" PageSize ="10">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelectActual_dur" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="action,actual_dur_key" DataNavigateUrlFormatString="{0}?id={1}"
DataTextField="post_code" HeaderText="Staff" SortExpression="post_code" meta:resourcekey="GridView2_post_code"/>
.....
</Columns>
<EmptyDataTemplate>
<span style="font-size: 14pt; color: #990000">
<asp:Localize runat="server" ID="text_no_record" Text="No records found." meta:resourcekey="text_no_record" />
</span>
</EmptyDataTemplate>
</asp:GridView>
<asp:Button ID="btnBatchUpdateActualDur" runat="server" Text="Batch Recommend/Approve Actual Duration"
OnClick="btnBatchUpdateActualDur_Click"
OnClientClick="return IsSelectedAtleastOneActualDur();"
Visible="false" Width="276px" meta:resourcekey="btnBatchUpdateActualDur"/>