AJAX & .Net
AJAX is abbreviation of "Asynchronous JavaScript and XML". AJAX is based on JavaScript and HTTP requests. Using Ajax in .Net is really simple and easy. Very basic steps:
Suppose our class name of webform is : MyWebForm1
(1) Add Ajax.dll in website project referrence.
(2) Register class on PageLoad for Ajax.
(3) Mark methods that can be called through AJAX.
(4) Call server side methods : Synchronously/ASynchronously
(5) Manipulate returned value
Here we go:
(1) Add Ajax.dll in website project:
As we add any other reference in website, add this reference also :)
(2) Register class for Ajax use on page load:
In page load event handler : (C#)
protected void Page_Load(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e)
add: Ajax.Utility.RegisterTypeForAjax(typeof(MyWebForm1));
(3) Mark method that can be called through AJAX: (C#)
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public int GetSquareAjax(int val) { return val * val; }
(4a) In JavaScript : To call a server side method ASynchronously, call method
as
as
val returnValue = MyWebForm1.GetSquareAjax(44).value;
where
- MyWebForm1 = Type
registered for AJAX in Page Load of webform - GetSquareAjax
= MethodName - 44 = Parameter
Value - returnValue = will have returned value.
(4b) In JavaScript : To call a server side method ASynchronously, call method
as
as
MyWebForm1.GetSquareAjax(44, ServerCallBack_GetSquareAjax);
where
MyWebForm1 = Type registered for AJAX in Page Load of webform
- GetSquareAjax
= MethodName - 44 = Parameter
Value - ServerCallBack_GetSquareAjax
= Call back function (should be written in javaScript)
this Call back function can be like:
function ServerCallBack_GetSquareAjax(ajaxReturnValue){
}
Note in above function there is one parameter; named= ajaxReturnValue. This is object and
have following properties:
have following properties:
- request
- error
- value
- context
Most important amoung these
properties are
- value
- error
'value' will have return value (Note if object is returned from server then 'value' property will have all exposed
properties of returned object.
properties of returned object.
'error' will have error value (if there is any error on server side)
PS : Please note that due to
hosting issues of code4coder group, right now we cannot upload examples/code
right now. We are working on it.
hosting issues of code4coder group, right now we cannot upload examples/code
right now. We are working on it.
try using Anthem.Net
ReplyDelete:)
ReplyDeleteDo not forget to add
ReplyDeletein web.config file