Calling Code behind Function in JavaScript

Introduction

 
Today I was trying to access a list of data from my JavaScript code in an ASP.Net page; I Googled for the best solution for this and I found many solutions:
  1. Using Microsoft AJAX and access web services function.
     
  2. Using WCF services and access it directly from JavaScript code.
     
  3. Simple solution using the Page method.

Definition for page method

 
It is a public static method in the code-behind of an aspx page and is callable from the client script. Decorated with the [WebMethod] attribute and rendered as inline JavaScript.
  1. [System.Web.Services.WebMethod]   
  2. public static string GetEmpName(string empid)  
  3. {  
  4.    if (empid == "1")  
  5.    return "Mohamed";  
  6.    elseif (empid == "2")  
  7.    return "Ahmed"else return "Waleed"; }   
Calling Page Method From JavaScript Code
 
1. Create 2 Text Boxes to simulate and show the results:
  1. <h3>Enter The Employee ID Here: </h3>  
  2.    <asp:TextBox ID="txtEmpID″ runat="server">  
  3.    </asp:TextBox> <br />  
  4.    <asp:TextBoxID="txtEmpName″ runat="server" ReadOnly="True">  
  5.    </asp:TextBox> <br />  
  6.    <asp:Button ID="btnGetName" OnClick="return false;"runat="server" Text="Button"></asp:Button>  
2. Add JavaScript function to call the page method and return the employee name:
  1. <script language="javascript" type="text/jscript">  
  2.    // Call the page method and run the success function  
  3.    functionGetEmployeeName(parmValueControl ,returnValueControl )  
  4.    {  
  5.       var ctrl = document.getElementById(parmValueControl);  
  6.       // call server side Function  
  7.       PageMethods.GetEmpName(ctrl .value, SuccessFunction, FailedFunction, returnValueControl);  
  8.    }  
  9.    // set the returnValueControl value with the ContactName  
  10.    function SuccessFunction(empName, returnValueControl)  
  11.    {  
  12.       var empNameVar =document.getElementById(returnValueControl);  
  13.       returnValueControl.value = empNameVar ; }  
  14.       // alert message on some failure  
  15.    function FailedFunction(returnedMessage, returnValueControl)  
  16.    {  
  17.       alert(returnedMessage.get_message());  
  18.    }  
  19. </script>  
3. Add attributes OnClientClick to our button to call GetEmployeeName Function which call our code behind method:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    if (!Page.IsPostBack)  
  4.    {  
  5.       btnGetName.Attributes.Add( "OnClientClick""javascript:GetEmployeeName('" + txtEmpID.ClientID + "', '" + txtEmpName.ClientID + "')");  
  6.    }  
  7. }  
4. Don't forget to add this attribute to your script manager:
  1. <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"/>  
Try now to run the page and click its button add 1,2 or 3 to the first text box and receive its value in employee name:) Hope this article helps a lot of people trying to pass values from C# code to show in a their html.


Similar Articles