Users Pricing

articles

home / developersection / articles / using the callback

Using the Callback

Uttam Misra 6790 09 Oct 2010 Updated 04 Mar 2020

The Callback feature that enables you to retrieve page values and populates them to an already generated page without regenerating the page.  This capability makes it possible to change values on a page without going through the entire postback cycle.

It uses the following method: 
  • Page.ClientScript.GetCallbackEventReference()
  • Page.ClientScript.RegisterClientScriptBlock()
  • GetCallbackResult()
  • RaiseCallbackEvent()

And it inherites class is System.Web.UI.ICallbackEventHandler

How we implement the Callback

Example:

Step1: we use the following code on aspx page

 <head runat="server">
    <title>Callback</title>
    <script type="text/javascript">  
    function ss()
    {  
    var va=document.forms[0].Text1.value;
    UseCallback(va,"");
    }
    function amit(Text1,context)
    {
    alert(Text1);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="Text1" type="text" runat="server" /><br />
      <input id="Button1" type="button" value="button" onclick="ss()"/>
    </div>
    </form>
</body>

  Step2: We write the following code on aspx.cs page. 

 public partial class y : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
    private string aa = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        string cref = Page.ClientScript.GetCallbackEventReference(this, "arg", "amit", "context");
        string cscript = "function UseCallback(arg,context)" +
            "{" + cref + ";" + "}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cscript, true);
 
    }
    public string GetCallbackResult()
    {
        return aa;
    }
    public void RaiseCallbackEvent(string eventArgument)
    {
        aa = eventArgument;
    }
}

Step3: Run the project

Output: 

Using the Callback


Uttam Misra

Information Technology

More than 18 years of working experience in IT sector. We are here to serve you best.