Click here to Skip to main content
15,892,517 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 78K   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
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NonStatic : System.Web.UI.Page,ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        RegisterCallBackReference();
    }
    /// <summary>
    /// Sever Side Method to call
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
    private String ServerSideMethod(String args)
    {
        String arg = args;
        if (arg == "Today")
            return Convert.ToString(DateTime.Now.Date);
        else if (arg == "Tomarrow")
            return Convert.ToString(DateTime.Now.Date.AddDays(1));
        return "";
    }

    #region "Call back Registration"
    private void RegisterCallBackReference()
    {

        String callBack = Page.ClientScript.GetCallbackEventReference(this, "arg",
             "CallbackOnSucceeded", "context", "CallbackOnFailed", true);
        //String callBack = Page.ClientScript.GetCallbackEventReference(this, "arg",
        //   "", "context", "", true);
        String clientFunction = "function CallServer(arg, context){ "
                       + callBack + "; }";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                "Call To Server", clientFunction, true);



    }
    #endregion


    #region ICallbackEventHandler Members
    String returnValue;
    string ICallbackEventHandler.GetCallbackResult()
    {
        return returnValue;
    }

    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        returnValue = ServerSideMethod(eventArgument); 
    }
    
    
    #endregion
}

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