Script Callback

What is the difference between a callback and a postback?

A callback is a special postback, so a round-trip always occurs; however, unlike the classic postback, the script callback doesn't redraw the whole page. ViewState is not updated during a callback, it is for postback.

How to make a callback?

In the client side JavaScript code, if GetCallbackEventReference() method is reference, then when the JavaScript code is executed, a channel to the server is opened and an HTTP request is sent to the remote ASP.NET page.

How does the ASP.NET runtime know this HTTP request is a Callback rather than a Postback?

After the ASP.NET runtime get a HTTP request, it looks for a __CALLBACKID entry in the Request collection. If such an entry is found, the runtime concludes that a callback invocation is being made.

GetCallbackEventReference() syntax

public string GetCallbackEventReference (

string target,      

    string argument,     

string clientCallback,      

    string context,

    string clientErrorCallback,

    bool useAsync

)

there are overloads such as

ClientScriptManager.GetCallbackEventReference (Control, String, String, String)

ClientScriptManager.GetCallbackEventReference (Control, String, String, String, Boolean)

GetCallbackEventReference() Parameters

target

The name of a server Control that handles the client callback. The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent method (which could be the page itself).

argument

An argument passed from the client script to the server RaiseCallbackEvent method.

clientCallback

The name of the client event handler that receives the result of the successful server event.

context

Client script that is evaluated on the client prior to initiating the callback. The result of the script is passed back to the client event handler.

clientErrorCallback

The name of the client event handler that receives the result when an error occurs in the server event handler.

useAsync

true to perform the callback asynchronously; false to perform the callback synchronously.

This article is part of the GWB Archives. Original Author: Aaron Li

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.