Chriz L

Chriz L

  • NA
  • 220
  • 50.8k

Jquery how to pass GridView as a parameter to a WebMethod

Sep 15 2016 2:16 AM
Hello,
 
I'm trying to pass a GridView as parameter from Javascript to a WebMethod. Here's my code:
 
  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>  
  2. <script type = "text/javascript">  
  3.     function bindGrid() {  
  4.         var pageurl = '<%=ResolveUrl("Default.aspx/bindGrid1") %>';  
  5.         var gv = $("#<%=gvDisplay.ClientID%>").val();  
  6.         var idno = $("#<%=txtIDNO.ClientID%>").val();  
  7.         var name = $("#<%=txtName.ClientID%>").val();  
  8.         var tel = $("#<%=txtTel.ClientID%>").val();  
  9.   
  10.         var parameter={"gv":gv, "idno":idno,"name":name,"tel":tel}  
  11.     $.ajax({  
  12.         type: "POST",  
  13.         url: pageurl,  
  14.         data: JSON.stringify(parameter),  
  15.         contentType: "application/json; charset=utf-8",  
  16.         dataType: "json",  
  17.         success: OnSuccess,  
  18.         failure: function(response) {  
  19.             alert(response.d);  
  20.         }  
  21.     });  
  22. }  
  23.     function OnSuccess(response) {  
  24.         alert(response.d);  
  25.     }  
  26. </script>  
and the WebMethod:
  1. [WebMethod]  
  2. public static string bindGrid1(GridView gv, String idno, String name, String tel)  
  3. {  
  4.      string message = "";  
  5.      DataSet ds;  
  6.      SqlDataAdapter SqlAda;  
  7.      SqlConnection con = getConnection();  
  8.      SqlCommand cmd = new SqlCommand();  
  9.      cmd.CommandType = CommandType.StoredProcedure;  
  10.      cmd.CommandText = "sproc";  
  11.      cmd.Parameters.Add("@IDNO", SqlDbType.VarChar).Value = idno;  
  12.      cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = name;  
  13.      cmd.Parameters.Add("@Tel", SqlDbType.VarChar).Value = tel;  
  14.      cmd.Connection = con;  
  15.      try  
  16.      {  
  17.          con.Open();  
  18.          gv.EmptyDataText = "No data.";  
  19.   
  20.          SqlAda = new SqlDataAdapter(cmd);  
  21.          ds = new DataSet();  
  22.   
  23.          SqlAda.Fill(ds);  
  24.   
  25.          gv.DataSource = ds;  
  26.          gv.DataBind();  
  27.          message = "success";  
  28.      }  
  29.      catch (Exception ex)  
  30.      {  
  31.          appObj.myMessageBox(ex.Message);  
  32.          message = "error";  
  33.      }  
  34.      finally  
  35.      {  
  36.          con.Close();  
  37.          con.Dispose();  
  38.      }  
  39.     return message;  
  40. }  
 I know I can't pass it as a string, but I can't find a way to pass it.
Any kind of help would be appreciated.
 
Thank you in advance. 

Answers (1)