Server side callback function ajax web service asp.net - asp.net

I'm using Web Service to handle ajax request in my project. I need to call a server side function to count users online when a new request is proceed.

If you want to call Server side method into Client side then,you have to transform this method as a PageMethod and then call this method i.e GetOnlineUser() from client side code; i.e. using JavaScript.
To enable the method as a PageMethod, add the attribute [WebMethod] on top of the GetOnlineUser method in .aspx code behind file.
if you are using asp.Net membership provider then simply call Membership.GetNumberOfUsersOnline().
And if you are not using membership you have to implement your own custom counter...

Related

JSON ASP.NET static methods

How come the method called from Client-side has to be a static method? The issue i am facing is that if the method is static then I can't access my server side controls such as CheckBoxPanel.
Is there any workaround this?
The method is static so that the server doesn't need to create an instance of the Page class just to run the method.
Even if the method wasn't static, you wouldn't be able to use the controls in the page anyway, because the form data in the page isn't posted to the server when you make a PageMethod call. You need to send all the information that the method needs in the call itself.

How Do I Get RouteData Values from a Web Service in .Net 4.0

I am trying to extract an id number from a URL using a web service so that it can be used as a parameter for a where clause in a select statement that produces data from a database based on the id number of a record. That data will then be passed back to the page to populate an element in a jQuery modal popup widow.
Everything works fine with a static id number (ex: string postid = "120"), but I don't know how to get the id number from the URL. I'm using Routing in .Net 4 and the method for accessing Routing in pages does not work in a web service. In pages I just do stuff like var id = RouteData.Values["id"]; and that gets the id, but when i did it in a web service I got an error:
CS0120: An object reference is required for the non-static field,
method, or property 'System.Web.Routing.RouteData.Values.get'
Summary:
I have web service accessed form a details page where I want to get RouteData for the page making the request. I want to do this just as easily as I can on a page using RouteData.Values which is just as easy as the now the obsolete Request.Querystring.
Now I am more confused because although I could easily add a new route for the web service I don't know I would call that using jQuery Ajax because of the webservice.asmx/webmethod syntax.
Right now I have URL: "../webservices/googlemaps.asmx/GetGoogleMap" in my jQuery Ajax, but that is not a real URL. It only exists in jQuery somewhere and the way to call the service using just JavaScript is no a real URL either, its webservice.webmethod() which in this case would be googlemaps.GetGoogleMap().
I will try registering a route for webservices/googlemaps.asmx/GetGoogleMap/postid, but I doubt it will work because GetGoogleMap is not a directory or a querystring.
Get current http request and use RequestContext property to get request context - it has current routing data. For example,
var id = HttpContext.Current.Request.RequestContext.RouteData.Values["id"];
In case of WCF based web service, make sure that service is participating in ASP.NET pipeline (see ASP.NET Compatibility)
EDIT: Sorry for misleading answer - the above will not work unless web service url is registered in routing engine. However, it may not solve your issue of retrieving the id - what kind of service implementation are you using? Are you making a GET request or POST request? Typically, web service handler (asmx) or WCF pipeline should convert GET/POST parameters to method parameters. Post your web service code and how you invoke it.

call my web services from other app with javascript?

I got .asmx a web service on my app. I need to call a method from an other app to get statistics from my app. I need it to return XML. the call to the webmethod is done with javascript soap.
EDIT
I got the web service working. I can execute code and return a string but it stops there. When I try to pass parameters into the method it wont work and when I try to return a string[] or any other type it wont work either. any ideas? Is there something I need to do passing in parameters?
I think you could do two things.
One: enable [ScriptService] attribute on web method. This allows you to call the webservice
with javascript.
Example
Two: enable http-post/http-get webservice calls
How to enable

How to share the web application session with a webservice

Here's the scenario. My page loads with certain objects in the session. The user clicks a button, and I need to update a certain section of my page using jquery. The jquery would make an ajax call to the web service which needs to access one of the objects in the session. How would I do this? How can my web service access the current session object?
update: I'm using asmx
all you do is set (EnableSession = true) on your webmethod attribute.
If your building a WCF service you can set this attribute on the class:
[AspNetCompatibilityRequirements
(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
Which should give you access. If your using asmx I don't remeber off the top of my head

How to call a webmethod asynchronously and partially render a control?

I need to call a webmethod of a webservice asynchronously from code behind of a web page.
In the callback function I need to bind a gridview and render it. I want to partially render that gridview in the callback function in codebehind.
How to implement that?
Is it possible to implement all these in codebehind without using javascript?
There are a couple of options, but basically you need to do something like this:
Use Visual Studio to build a proxy class to access the web service, using the published WSDL
Create an async web page, by setting Async=True in the Page directive
In the Page_Load() method of your code behind, register methods that will start and end the async web service call by creating a PageAsyncTask object and calling RegisterAsyncTask()
From the method that starts the async task, call the Begin method that was created as part of the proxy class, and return the associated IAsyncResult to the caller
When the web service call completes, the runtime will call your registered end method. From there, call the End method in the proxy to get the results of the call.
Databind the results to a GridView on your page.
In case it helps, I walk through a detailed example along these lines in my book, including sample code: Ultra-Fast ASP.NET.
You can use ASP.NET asynchronous page load for that.
In general, it consists of adding Async="true" to the page directive and adding some event handlers in the code behind.
Great resource about this subject is the "Asynchronous Pages in ASP.NET 2.0" MSDN Magazine article.

Resources