You cannot call server-side code ‘directly’ from client-side code. This is a problem which i faced during a project. Finally i found the solution for it. So i thought of telling you the solution, which might help you too.
In my case im a taking date from the client side and passing it to the server side. And want to execute a method in server side and return a value to client side according to the value i passed from client side.
So lets see how it goes...................
Following is my javascript method which i wrote to execute the server side method in Calender.aspx .
which contains a method called GetSchedule().
The value Im passing is key. You can reference at server side as TDate.
function getSchedule(key) {
var returnMessage = '';
$.ajax({
async: false,
type: 'POST',
url: './Calender.aspx/GetSchedule',
data: '{"TDate":"' + key + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(d, status) {
if (d.d == null) { returnMessage = '';}
else{
returnMessage = d.d;}
},
error: function(xhr, msg, e) {
alert(msg);
}
});
return returnMessage;
}
This my webMethod written in the code behind....
Don't forget to use the same reference name you used in the javascript method.
Do what ever you want to do inside the method and return the value.
<WebMethod()> _
Public Shared Function GetSchedule(ByVal TDate As String) As String
Dim sdate As DateTime = Convert.ToDateTime(TDate)
........................................
........................................................
.................................................................
Return com
End Function
In the javascript method you can get the return value using the parameter d
Hope this will be helpful for you guys.
Cheers........:)
No comments:
Post a Comment