Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0 C#3.5 C#5
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 6-Feb-12 22:32pm
Edited 2-Jul-14 1:31am
Rahul VB4.7K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.......
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
}
}
  Permalink  
v2
Comments
VICK at 3-Jul-14 1:16am
   
Any perfect logic to just copy the whole code from the link in already posted solution???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Dear--
Calling from client side to server side.
take one button and there onclick event and put server side function inside the click event.
function CallpageBasedonSelectFromChildPage() {
           __doPostBack('<%=btn1.UniqueID %>', '');
       }
 

Calling from server side to client side
 

e.Cell.Attributes.Add("onMouseOver", "javascript:ShowModalPopup('" + CS + "', '" + NC + "','" + CE + "','" + IP + "');");
                  e.Cell.Attributes.Add("onMouseOut", "javascript:HideModalPopup();");
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 22 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100