Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,


Is it possible to call a method in the JavaScript?

The method is not a web method.i.e please look the method bellow

C#
public static string LoadData()
  {
      SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
      SqlDataAdapter da = new SqlDataAdapter("select * from Employee", cn);
      DataSet ds = new DataSet();
      da.Fill(ds);
      return JsonConvert.SerializeObject(ds.Tables[0]);
  }


I need to call this method in script. I have done with we method but our manager is not accepting because If we declare method as a web method it exposed so that I need to call direct method in script i.e JQGrid Script.Please look the script

JavaScript
<pre lang="cs">$(document).ready(function () {
                        $.ajax({
                url: "Default1.aspx/LoadData",
                datatype: "json",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                method: "POST",
                success: function (result) {
                    result = result.d;
                    jQuery("#EmpTable").jqGrid
                                                    ({
                                                        datatype: "local",
                                                        colModel: [
                                                                    { name: 'Id', index: 'Id', width: 50 },
                                                                    { name: 'FirstName', index: 'FirstName', width: 150 },
                                                                    { name: 'LastName', index: 'LastName', width: 150 },
                                                                    { name: 'Last4ssn', index: 'Last4ssn', width: 60 },
                                                                    { name: 'Department', index: 'Department', width: 80 },
                                                                    { name: 'Age', index: 'Age', width: 50 },
                                                                    { name: 'Salary', index: 'Salary', width: 80 },
                                                                    { name: 'Address', index: 'Address', width: 150 },
                                                                    { name: 'MaritalStatus', index: 'MaritalStatus', width: 100 }
                                                                  ],

                                                        data: JSON.parse(result),
                                                        rowNum: 2,
                                                        loadonce: true,
                                                        rowList: [2, 5, 10],
                                                        pager: '#EmpPager',
                                                        viewrecords: true,
                                                        sortname: 'id',
                                                        gridview: true,
                                                        sortorder: 'desc',
                                                        mtype: 'GET',
                                                        height: "auto",
                                                        caption: "List Employee Details",
                                                        editurl: "Default1.aspx/LoadData"
                                                    });
                    // pager added
                    $('#EmpTable').jqGrid('navGrid', '#EmpPager',
                                                       {
                                                           edit: true,
                                                           add: true,
                                                           del: true,
                                                           search: true,
                                                           searchtext: "Search",
                                                           addtext: "Add",
                                                           edittext: "Edit",
                                                           deltext: "Delete"
                                                       }, { closeOnEscape: true, reloadAfterSubmit: true,
                                                           drag: false
                                                       });
                   
                }
                
            });
            
        });</pre>

If You don't Know the jqGrid leave it the jqGrid script. In general script how we can call. 

Thanks,
Purna
Posted

Hi,

I have find the answer. We can able to perform by using query string.In page load we need to check the query string value if it is matched we can able to call the required method.
Pass the url like
JavaScript
url: "Default1.aspx?x=LoadData"



Thanks,
Purna
 
Share this answer
 
you can call static method like this.. :)

Default1.aspx

JavaScript
$(document).ready(function () {
  $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Default1.aspx/LoadData",
        data: DataValue,
        dataType: "json",
        success: function (data) {

          alert("Success");

        },
        error: function (result) {

          alert("error");

        }
    });
});


Default1.aspx.cs

C#
    [WebMethod]
public static string LoadData()
   {
       SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
       SqlDataAdapter da = new SqlDataAdapter("select * from Employee", cn);
       DataSet ds = new DataSet();
       da.Fill(ds);
       return JsonConvert.SerializeObject(ds.Tables[0]);
   }
 
Share this answer
 
Comments
Purna.N 29-May-14 1:04am    
I don't want to declare method as a web method.With out declaring the web method how we can call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900