65.9K
CodeProject is changing. Read more.
Home

How to Call the Code Behind Method in Javascript

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.91/5 (6 votes)

Aug 6, 2010

Ms-PL
viewsIcon

27270

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");
    }

}