How to call a webmethod asynchronously and partially render a control? - asp.net

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.

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.

Why WebMethod declared as Static?

I declared a WebMethod in my default.aspx.cs file..
[WebMethod]
public static void ResetDate()
{
LoadCallHistory(TheNewDate.Date);
}
Why must the WebMethod method be declared static?
They're static because they are entirely stateless, they don't create an instance of your page's class and nothing is passed to them in the request (i.e. ViewState and form field values).
HTTP is stateless by default, ASP.Net does a lot of stuff in the background with ViewState, Session, etc. during a standard page request to make life easier for developers.
When a web method is called through AJAX, the page isn't sending all the necessary form data ASP.Net embeds in a page to keep track of request state because it would make web methods too slow; and if you need to do a lot of processing you should move it out to a dedicated web service instead.
You can get access to methods on the page using HttpContext.CurrentHandler which is explained in more detail here and also the current user if you need it via HttpContext.Current.User.
There's an excellent article here explaining this in more detail.

Have Page Method Unhandled Exceptions Behave as Other ASP.Net Unhandled Exceptions

I have a webform that has a single page method. My other web apps log unhandled exceptions to the system event log on the web server. Since the other developers that I work with expect to see entries for app errors in the event log, I would like this app to do the same.
However, I have the app send error emails when an exception is caught from calling code inside the page method. It is not writing to the event log when this occurs. Note: the page method re-throws the exception after calling my email notification method.
From what I've read so far it seems that ASP.Net logs errors to the event log by default. I imagine that the same is not true for Page Methods/WebMethods because they basically throw the exception to the client code calling it.
Is there a trivial way to have that exception bubble up properly so that it writes to the event log? No other apps write to the event log directly from what I've seen so I don't think the application could create a new source since our security people keep a lot of things locked down (with good intentions, yay security).
[WebMethod]
public static object MyPseudoWebMethod()
{
try
{
// My exception spawning unreliable code here
}
catch(Exception ex)
{
// Cleanup ...
this.SendErrorNotification(ex);
throw; // <-- This doesn't bubble up but I'd love for it to!
}
}
Hmm interesting problem. You are right in that WebMethod exceptions do NOT follow normal exception flow.
The Application_Error event is not fired if your web method throws an
exception. The reason for this is that the HTTP handler for XML Web
services consumes any exception that occurs while an XML Web service
is executing and turns it into a SOAP fault prior to the
Application_Error event is called.
(from here)
The above page suggests using a SOAP extension to catch that exception before its swallowed, but here's how I'd do it if you don't want to do that:
1) Make a new 'error recieving' ASPX page that you will build that will take whatever params you want to record in your error log. So for example, have this page take in a POST named "ExceptionDetails" or whatever else you wish to capture. This page will NOT be viewed directly in a browser, so it doesnt need any ASPX controls or anything, but using a MasterPage on it won't hurt anything.
2) In the code behind of this new page, grab whatever POSTS you are sending in and new-up an Exception with whatever details you need. Immediate throw this exception. Doing this means that this exception will follow whatever flow other unhandled exceptions follow in the app (logging, emailing, etc).
3) On the page that calls the WebMethod JS, Wrap the calls to the WebMethod in a try-catch
4) In the catch block, print out whatever message you want in the browser, and initiate a new AJAX post to that new error receiving ASPX page, passing along whatever POST stuff you made that page look for.
The new AJAX call will NOT change ANYTHING in the user's perception by default. The AJAX call fires off a new request to that page, and the ASPX page itself is actually totally unaware that its AJAX and not a normal browser request. Any cookies/session/authentication data that's currently set are available to the AJAXed page as well, if you are recording a user's ID or anything. If you look at the returned response from a tool like Firebug, you will see that its actually the YellowScreenOfDeath's HTML (unless you have a custom 500 page, in which case its that HTML that comes back).
This is simply how the legacy ASMX web services work.
The only workaround is to stop using them (which you should do anyway, unless you're stuck with .NET 2.0). WCF doesn't have this problem.

Server side callback function ajax web service 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...

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

Resources