Check For CheckBox Status [Resolved]

Posted by Sharpcnet under JavaScript on 12/14/2013 | Points: 10 | Views : 1775 | Status : [Member] | Replies : 1
Using ASP.Net, C#.

This is a javascript function to ensure that atleast one of the checkboxes in the grid is checked, before perfoming the save Operation. By Default at page load, all the boxes are checked.

Problem: When user clicks 'Save' (and no check box was checked), it prompts the alert. On clicking OK inside the alert, it refreshes the page, and thereby clearing the textboxes inside the grid. Also checking all the checkboxes again.

I would like to just show the alert and not refresh the page, cause this is causing the server side code to start even when checkboxes werent checked.
JS:
<script type="text/javascript">
function CheckSelected()
{
var elements = document.getElementById('<%=gvLeaves.ClientID%>').document.getElementById("INPUT");
var status=false;
var chk;
for(var i=0;i<elements.length;i++)
{
if(elements[i]!=null)
{
if(elements[i].id.indexof('chkLeaveType')>0)
{
chk=elements[i];
if(chk.checked)
{
status=true;
break;
}
}
}
}
if(status==true)
return true;
else
{
alert('None Selected');
return false;
}
}
</script>
ASPX:

<asp:updatepanel runat="server" updatemode="conditional">
<triggers>
<asp:asyncpostback controldid="btnsave" eventname="click"/>
</triggers>
<contenttemplate>
<asp:gridview id="gvleaves" runat="server">
<columns>
<asp:templatefield>
<headertemplate>
<asp:checkbox id="chkheader" runat="server" checked="true" onclick="SelectAll(this.id)"/>
Leave Type
</headertemplate>
<itemtemplate>
<asp:checkbox id="chkLeaveType" runat="server" checked="true"/>
<%#Eval("leavetype")%>
</itemtemplate>
</asp:templatefield>
<asp:templatefield>
<itemtemplate>
<asp:textbox id="txtdays" runat="server"/>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<asp:button id="btnSave" runat="server" onclientclick="CheckSelected();" onclick="btnsave_click"/>
</contenttemplate>
</asp:updatepanel>





Responses

Posted by: Sharpcnet on: 12/14/2013 [Member] Starter | Points: 25

Up
0
Down

Resolved
I found this solution from another forum and It works.
<asp:button id="btnSave" runat="server" 
onclientclick="return CheckSelected();" onclick="btnsave_click"/>





Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response