SIGN UP MEMBER LOGIN:    
ARTICLE

Calling Code behind Function in JavaScript

Posted by Waleed Mohamed Articles | ASP.NET Programming December 15, 2009
In this article we see how to call C# Function from JavaScript.
Reader Level:


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 Micorosoft AJAX and access web services function 
  2. Using WCF services and access it directly from JavaScript code 
  3. Simple solution using Page method

A) 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.


[System.Web.Services.WebMethod] 

public static string GetEmpName(string empid)

{

 if (empid == "1")

 return "Mohamed";

 elseif (empid == "2")

 return "Ahmed"; else return "Waleed"; } 


A) Calling Page Method From JavaScript Code :


1- Create 2 Text Boxes to simulate and show the results :


<h3>Enter The Employee ID Here: </h3>
 <
asp:TextBox ID="txtEmpID″ runat="server">
 
 </asp:TextBox> <br />
 
 <asp:TextBoxID="txtEmpName″ runat="server" ReadOnly="True">
 </
asp:TextBox> <br />
  <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 :


<script language="javascript" type="text/jscript">

 // Call the page method and run the success function

 functionGetEmployeeName(parmValueControl ,returnValueControl )

 {

 var ctrl = document.getElementById(parmValueControl);

 // call server side Function

 PageMethods.GetEmpName(ctrl .value, SuccessFunction, FailedFunction, returnValueControl);

 }

 // set the returnValueControl value with the ContactName

 function SuccessFunction(empName, returnValueControl)

 {

 var empNameVar =document.getElementById(returnValueControl);

 returnValueControl.value = empNameVar ; }

 // alert message on some failure

function FailedFunction(returnedMessage, returnValueControl)

 {

 alert(returnedMessage.get_message());

 }

 </script>


3- Add attributes OnClientClick to our button to call GetEmployeeName Function which call our code behind method:


protected void Page_Load(object sender, EventArgs e)

 {

 if (!Page.IsPostBack)

 {

 btnGetName.Attributes.Add( "OnClientClick", "javascript:GetEmployeeName('" + txtEmpID.ClientID + "', '" + txtEmpName.ClientID + "')");

 }

 }


3- Don't forget to add this attribute to your script manager:


<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.

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

really it's a simple and easy solution to call server side method through javascript

Posted by Manish Dwivedi Jan 31, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor