Search code examples
ajaxvb.netcheckboxcomboboxshared

Sending checkbox data through AJAX in vb.net


I am trying to send some data from a .aspx form to a function in its .vb file. I can successfully send data of text box or dropdown menus but when I try to send data of checkbox (whether checked or not) it shows an error

"undefined error Internal Server Error"

I have also tried it using var advprev = $('#<%=advprev.Checked %>').val(); but still it shows same error.

Below is my ajax code and vb.net function as well. (advprev is my checkbox)

AJAX

$(document).ready(function () {

          $('#<%=btnGeneratePay.ClientID %>').click(function () {

              var comboMonth = $('#<%=comboMonth.ClientID %>').val();
              var comboYear = $('#<%=comboYear.ClientID%>').val();

              var txtEmpNoFrom = $('#<%=txtEmpNoFrom.ClientID %>').val();
              var txtEmpNoTo = $('#<%=txtEmpNoTo.ClientID%>').val();

              var advprev = $('#<%=advprev.ClientID %>').val();

              $.ajax({
                  type: "POST",
                  url: "GeneratePay.aspx/Update",
                  data: "{'advprev':'" + advprev + "','comboMonth':'" + comboMonth + "','comboYear':'" + comboYear + "','txtEmpNoFrom':'" + txtEmpNoFrom + "','txtEmpNoTo':'" + txtEmpNoTo + "'}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (Response_msg) {
                      if (Response_msg.d.indexOf("ERROR") < 0) {
                          $('#divStatus').html(Response_msg.d)
                          $('#divStatus').css({ "font-weight": "bold", "color": "Green" })


                          $('#divStatus').fadeIn(1)
                          $('#divStatus').fadeOut(5000);
                      }
                      else {
                          $('#divStatus').html(Response_msg.d)
                          $('#divStatus').css({ "font-weight": "bold", "color": "Red" })
                          $('#divStatus').fadeIn(1)
                          $('#divStatus').fadeOut(5000);
                      }
                  },
                  error: function (xhr, status, errorThrown) {
                      $('#divStatus').html(errorThrown + '</br>' + status + '</br>' + xhr.statusText)
                      $('#divStatus').fadeIn(1)
                      $('#divStatus').fadeOut(5000);
                  }
              });
              return false;
          });
      });

VB.net function

Public Shared Function Update(ByVal advprv As String, ByVal comboMonth As String, ByVal comboYear As String, ByVal txtEmpNoFrom As String, ByVal txtEmpNoTo As String) As String

Solution

  • Ok so I have figured it out. I have used the following technique to get checkbox data (whether checked or not)

    var advprev = $('#<%=advprev.ClientID %>').is(':checked');
    

    It returns true if checked and false if unchecked.