Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET
Article

How to Call the Code Behind Method in Javascript

Rate me:
Please Sign up or sign in to vote.
1.91/5 (6 votes)
5 Aug 2010Ms-PL 26.8K   7   6
Calling the Asp.net Code Behind method into Javascript

Introduction 

How to access the Code behind method into java script.

Background 

It will useful into some block of code in between to show the alert or confirmation message
 

Using the code

  Blocks of code should be set as style "Formatted" like this:

<script language="javascript" type="text/javascript">
      function call_me()
      {

         var Temp= confirm("Hit from ClientSide");

           if(Temp)
           {
           alert("True");
            __doPostBack('btnHdn','onserverclick');
           }
           else
           {
           alert("false");
           }

      }
  </script>
From Button event
<input type="button" runat="server" onclick="call_me();" id="btnClick" value="PRESS" /> 
Calling this method into  java script function.
public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Call_Server(Object sender, EventArgs e)
    {
        Response.Write("Calling Server Side Event");
    }

}

 

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect
India India
My interest areas are WCF,.net,Generics,Enterprise Library,Regular expressions,Silver light and Windows Workflow Foundation.Product Development and Architecture

Comments and Discussions

 
GeneralMy vote of 3 Pin
Ziad Elmalki2-Jan-11 23:24
Ziad Elmalki2-Jan-11 23:24 
GeneralMy vote of 1 Pin
damiaan7-Aug-10 4:41
damiaan7-Aug-10 4:41 
GeneralMy vote of 1 Pin
j03x26-Aug-10 21:34
j03x26-Aug-10 21:34 
GeneralMessage Removed Pin
6-Aug-10 12:37
User 30424986-Aug-10 12:37 
GeneralMy vote of 2 Pin
Gil Fink6-Aug-10 3:14
Gil Fink6-Aug-10 3:14 
This is not an article. An article explains in details what the code is doing, describes the purpose of the code and also has a beginning, body and a summary at least. This is more appropriate to be a tips & tricks.
GeneralNon Functional Pin
ryanoc3336-Aug-10 2:41
ryanoc3336-Aug-10 2:41 
General[My vote of 2] Looks like a Tips & Tricks Pin
Kunal Chowdhury «IN»5-Aug-10 23:24
professionalKunal Chowdhury «IN»5-Aug-10 23:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.