Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET

How to call code behind methods / Server side script / Server side static methods / Non static Methods using java script / .Net

Rate me:
Please Sign up or sign in to vote.
2.32/5 (13 votes)
7 Oct 20072 min read 77.9K   1K   22  
The basic idea of designing this module is to give an idea to call server side methods or sever side script using java script and Asp.Net. In this example I am calling Static methods and non static methods using two different techniques. Ajax.Net , Client Callback
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NonStatic.aspx.cs" Inherits="NonStatic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Non Static Web Method Call</title>
   
</head>
<body>
    <form id="form1" runat="server">
   
        <input id="btnToday" onclick="GetDate(this)" type="button" value="Server Date" />
        <input id="btnTomarrow" onclick="GetDate(this)" type="button" value="Tomarrow Date" />
        <div id="divDisplayDate" style="padding-top: 50px;">
        </div>
        <div>
            Date:&nbsp<span id="DisplayDate"></span>&nbsp;&nbsp;Day:&nbsp;<span id="DisplayDay"></span>
        </div>
    </form>

    <script type="text/javascript">
    
    function GetDate(btn)
    {
     var context=new Object();
    
    switch(btn.id){
    case 'btnToday':
        context.flag='Today';
        break;
   case 'btnTomarrow':
        context.flag='Tomarrow';
        break;
        }
        //Registered from code behind as:function CallServer(arg, context)
          CallServer(context.flag,context);
    
    }
    
    function CallbackOnSucceeded(result,context)
    {
    switch(context.flag)
    {
    case 'Today':
        DisplayDate.innerHTML = result;
        break;
   case 'Tomarrow':
        DisplayDay.innerHTML = result;
        break;
     }
    }
   function CallbackOnFailed(result,context)
   {
   
   divDisplay.innerHTML =result;
   
   }
    
    </script>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer FCS Software Solutions Ltd., Noida ( INDIA )
India India
Love to develope web based applications using Microsoft technologies.

Comments and Discussions