Call code behind method from JavaScript in asp.net

There are ways to call a web service method JavaScript, for more detail you can see how to retrieve data from database using JavaScript in asp.net, but is there any way to call a normal method from JavaScript? And the answer is No; show how we can call a code behind method from JavaScript, let’s see this with code

Add following HTML on page:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />

<div>
   <div>Enter your name:</div>
   <div> 
       <asp:TextBox ID=”txtName” runat=”server” />
  </div>
  <div> 
       <asp:TextBox ID=”txtMessage” runat=”server” />
  </div>
  <div> 
       <asp:Button ID=”btnSave” runat=”server” Text=”Save”
          OnClientClick=”return CallCodeBehindMethod();” />
  </div>
</div>

Now time to adjust our code behind so we can call it from JavaScript, wee need to use System.Web.Services so add it in our code behind file

using System.Web.Services;

Whatever method we need to call from JavaScript, add WebMethodattribute to that method and that will easily be called by javaScript

[WebMethod]
public String GetName()
{
    if(DateTime.Now.Hour < 12)
      return "Good morning, " + txtName.Text;
   else if(DateTime.Now.Hour < 16)
      return "Good after noon, " + txtName.Text;
   else 
      return "Good evening, " + txtName.Text;  
}

Now we will call GetName from JavaScript, so add the following JavaScript to the page:

function CallCodeBehindMethod()
{
    pageName.GetName(OnSuccess, OnFailed);
}

function OnSuccess(val)
{
   if(val)     
       document.getElementById('txtMessage').value = val;
}
function OnSuccess(error)
{
   if(error)     
       document.getElementById('txtMessage').value = error;
}

As you can see we have not used web service but we changed a method to web method so it can be called from JavaScript but without converting a method into web method we can not call any code behind method from JavaScript.

Jonathan King
  • asp.net
  • javascript
By Jonathan King On 08 Apr, 13  Viewed: 12,486

Other blogs you may like

Readonly textbox postback issues and solutions

In many cases we need to use read only text box so user cannot edit the value but readonly textbox will lost its value after postback. Let’s say you have a read only text box to set date by using ajax calendar so after post back date will be lost, other case might be you are setting some value in... By Ali Adravi   On 24 Apr 2013  Viewed: 4,277

Gridview paginated data with search and sort functionality in asp.net

Most of the times we need to use GridView control to show tabular data in our asp.net application. We simply write procedure to search the records and bind them with GridView and show 10 to 20 records per page. Have you ever thought that why you are fetching all the records and show only 10 to 20... By Ali Adravi   On 16 Feb 2013  Viewed: 8,518

ASP.Net 4.5 new feature Model Binding

A nice feature with ASP.Net is the model binding, it reduced our code and effort to bind our well know controls like GridView, DataList, Repeater etc. So let’s see how we can bind our old controls in new and easy way. Let’s see how we bind the our grid before 4.5 1. We write the code to get... By Mike .Net   On 17 Jan 2013  Viewed: 3,209

Upload multiple image in multiple size with progress bar in asp.net

In asp.net there is not control to select multiple files and upload them once with progress bar, so we will use a small third party DLL to achieve this functionality. We will use Flajaxian FileUploader, you can download it from [http://www.flajaxian.com][1] We will create three different images... By Hamden   On 12 Jul 2012  Viewed: 6,558

Check/Uncheck all checkboxes in asp.net by javascript

I was searching for select/deselect all checkboxes code into a gridview. I found most of them only provide to select and deselet all the checkboxes if any of the checkbox is unselected into grid the main checkbox is not affecting. I further try to search some useful code but could not found any... By Ali Adravi   On 10 Jul 2012  Viewed: 5,419