0

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>

1 Answer 1

1

CheckSelected() should return true or false, if false value is returned action won't happen. So it should look like onclientclick="return CheckSelected()"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.