Click here to Skip to main content
16,020,565 members
Please Sign up or sign in to vote.
4.29/5 (5 votes)
Hi friends ,

I am right now doing a project, but i am facing a situation where i need to call a server function using javascript and server function calling a javascript function.

i just need references to proceed in my project

Thanks in Advance
Posted
Updated 2-Jul-14 0:31am
v2

JavaScript
Response.Output("<script language="javascript">nameOfJavascriptFunction();</script>")

// Javascript function
function CallSum() 
{
//Get the controls
var txt1 = $get("txt1");
var txt2 = $get("txt2");
var txtresult = $get("txtSum");

//Call server side function
PageMethods.Sum(txt1.value,txt2.value,OnCallSumComplete,OnCallSumError,txtresult);

//Server side function gets the 2 arguments arg1 and arg2. 
//We are passing txt1.value and txt2.value
//for that. OnCallSumComplete is callback function for complete successfully. 
//OnCallSumError is callback
//function on error. txtresult is usercontext parameter.

//OnCallSumComplete,OnCallSumError,txtresult are optional parameters.

//If server side code executed successfully, then OnCallSumComplete will call.
//If server side code do not executed successfully, then OnCallSumError will call.
}

// Callback function on complete
// First argument is always "result" if server side code returns void 
// then this value will be null
// Second argument is usercontext control pass at the time of call
// Third argument is methodName (server side function name) 
// In this example the methodName will be "Sum"
function OnCallSumComplete(result,txtresult,methodName)
{
//Show the result in txtresult
txtresult.value = result;
}

// Callback function on error
// Callback function on complete
// First argument is always "error" if server side code throws any exception
// Second argument is usercontext control pass at the time of call
// Third argument is methodName (server side function name) 
// In this example the methodName will be "Sum"
function OnCallSumError(error,userContext,methodName)
{
if(error !== null) 
{
alert(error.get_message());
}
}
// -->
</script>
 
Server Side Code:
 
/// <summary>
/// Server side function Sum
/// </summary>
/// <param name="arg1">arg1</param>
/// <param name="arg2">arg2</param>
/// <returns>result(sum)</returns>
[System.Web.Services.WebMethod]
public static int Sum(int arg1, int arg2)
{
//On server side we can do anything, like we can access the Session.
//We can do database access operation. Without postback.
try
{
return arg1 + arg2;
}
catch(Exception ex)
{
throw ex;
}
}
 
Share this answer
 
v2
Comments
VICK 3-Jul-14 1:16am    
Any perfect logic to just copy the whole code from the link in already posted solution???
If any one wants to be pass data from code behind to javascript then use WebMethod and PageMethod which normaly working as like core ajax when we have to create webmethod at code behind with return type as writen bellow code:


1. Call Web Method in Javascript using PageMethod technique:

Code Behind Page:
-----------------------------------------------------------
step 1: Create one Web Method in code behind page

[WebMethod]
public static int GetValue()
{
int no=10;
return no;
}

step 2: Create javascript function for call web method using PageMethod technique

function GetCodeBehind_Value()
{
PageMethods.GetValue(onsucess,onerror);
}


// These both method is normally used to handling javascript GetCodeBehind_Value() method which is return result value of "onsucess(result)" method if method is sucessfully called other then return value of "onerror(result)" method.
function onsucess(result)
{
alert(result);
}

function onerror(result)
{
alert(result);
}


Note: Plz use ScriptManage and enamblepageload method true.

<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" xmlns:asp="#unknown">


I hope it will help you.......
 
Share this answer
 
Dear--
Calling from client side to server side.
take one button and there onclick event and put server side function inside the click event.
XML
function CallpageBasedonSelectFromChildPage() {
           __doPostBack('<%=btn1.UniqueID %>', '');
       }



Calling from server side to client side


C#
e.Cell.Attributes.Add("onMouseOver", "javascript:ShowModalPopup('" + CS + "', '" + NC + "','" + CE + "','" + IP + "');");
                  e.Cell.Attributes.Add("onMouseOut", "javascript:HideModalPopup();");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900