Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Wednesday, November 16, 2011

Calling Server Side Code from Client(AJAX)

Mark the ScriptManager as below:-
Source:-
Use PageMethods.Method(Server)
http://forums.asp.net/t/1323698.aspx/1

Assume Page is a.spx

1.(Removed the greater and less than signs...you need to append them) On a.spx
asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
/asp:ScriptManager

2. This is the Client Click Event of Button(on aspx page) from where you would like to Call Server Side Code.
input type="button" id="btnSend" onclick="Send('a','b','c')"

3. Client Method on Aspx Page
function Send(coursme, startme,isme) {

var btn = document.getElementById("btnSend");
btn.disabled = true;

//Server Side Code Call
PageMethods.Send(coursme, startme, isme, Succeeded, Failed);
}

4. You should have a method in a.aspx.cs
[WebMethod]
//public static string Send(string id, int courseRoleId)
public static string Send(string coursme, string startme, string isme)
{
.......................
.......................
Code to Execute on Server
.......................
.......................
}

5. Hopefully you are all set...This is just to give you an idea....

Handling Ajax Errors a Simple Sample

You need to Modify this sample as this was not getting rendered and i didn't had much time to look so i removed the brackets but the methods are intact mostly...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
script
void handleClick(object sender, EventArgs e)
{
int a = 0;
int res = 10 / a;
}
void HandleError(object sender, AsyncPostBackErrorEventArgs e)
{
//here we could log the error, and see which exception we're getting in order to set a specific error message
//in this case, I'm just returning the current date/hour back
ScriptManager1.AsyncPostBackErrorMessage = "Ooops...error occurred: " + e.Exception.Message +
DateTime.Now.ToString();
}
/script



form id="form1" runat="server"
asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="HandleError" /
asp:UpdatePanel runat="server" ID="panel"
contenttemplate
asp:Button runat="server" ID="bt" Text="gerar erro no servidor"
OnClick="handleClick" /
/ContentTemplate
/asp:UpdatePanel
/form