JSON ASP.NET static methods - asp.net

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.

Related

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.

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...

Asp.Net Ajax - Call non-static method

From client side, I need to call a server method that is not static.
For example, I got the following user control ucData (private instance of code-behind) that is Databind in the load event.
The server method I need should return ucData.IsValid(). So it can't be static
Is there a way I can do that ?
No...because there is no instance on the server to call the method on. Once the page is generated and sent to the client, there is no more context and all instances are destroyed.
Your best option is going to be to:
Create a static method
Pass that method the info needed to create the instance of the object you need
Call the method on the instance you just created
Return the results from your static method.

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 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