Home > DeveloperSection > Articles > Using the Callback

Using the Callback


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 3844  View(s)
Rate this:

Using the Callback


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:

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick