ASP.NET - what is use of GetCallbackEventReference

Asked By balaji mogadali on 02-Sep-11 12:35 AM
hi frds

how to use GetCallbackEventReference in asp.net what is use of it
Riley K replied to balaji mogadali on 02-Sep-11 12:43 AM
Reference from MSDN

Obtains a reference to a client function that, when invoked, initiates a client call back to a server event. The client function for this overloaded method includes a specified control, argument, client script, and context.


To invoke an asynchronous client script callback, we must first obtain a reference or entry point to the javascript that invokes the asynchronous method. We can then register the event reference in the ASPX page, so it can be called as required by the client-side script.


To do so, we use the GetCallbackEventReference method that is part of the ClientScriptManager class. All the client-side scripting functionality resides in the ClientScriptManager class, including the RegisterClientScriptBlock and RegisterStartupScript methods that existed in the V1 and V1.1 framework. 


refer this links


http://msdn.microsoft.com/en-us/library/ms153103.aspx

http://www.simple-talk.com/dotnet/asp.net/asynchronous-client-script-callbacks/

Regards

Web Star replied to balaji mogadali on 02-Sep-11 12:48 AM
use of GetCallbackEventReference is Obtains a reference to a client function that, when invoked, initiates a client call back to a server event. The client function for this overloaded method includes a specified control, argument, client script, and context.
see how can use it in asp.net application
http://www.aspxcode.net/free-asp-net-sample-source-code.aspx?Topics=How%20to%20use%20JavaScript%20by%20GetCallbackEventReference
Jitendra Faye replied to balaji mogadali on 02-Sep-11 01:12 AM
The GetCallbackEventReference()  method performs an out-of-band callback to the server that is a modified version of a page's normal life cycle.


Obtains a reference to a client function that, when invoked, initiates a client call back to server events. The client function for this overloaded method includes a specified target, argument, client script, context, error handler, and Boolean value.

Namespace:  http://msdn.microsoft.com/en-us/library/system.web.ui.aspx
Assembly:  System.Web (in System.Web.dll)

Example-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;


public partial class How_to_use_Callback_c : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    private string CallbackResult = null;
    protected void Page_Load(object sender, EventArgs e)
    {
      string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetRandomNumberFromServer", "context");
      string cbScript = "function UseCallback(arg, context)" + "{" + cbReference + ";" + "}";
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cbScript, true);
    }
    public void RaiseCallbackEvent(string eventArgument)
    {
      Random rnd = new Random();
      CallbackResult = rnd.Next().ToString();
    }
    public string GetCallbackResult()
    {
      return CallbackResult;
    }
}


Try this and let me know.


Sreekumar P replied to balaji mogadali on 02-Sep-11 01:31 AM
Hi,

ClientScriptManager.GetCallbackEventReference Method (String, String, String, String, String, Boolean)
      Obtains a reference to a client function that, when invoked, initiates a client call back to server events. The client function for this overloaded method includes a specified target, argument, client script, context, error handler, and Boolean value.

here is a example.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="RandomNumber" %>
 
<!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 id="Head1" runat="server">
  <title>Callback Page</title>
   
  <script type="text/javascript">
    function GetNumber(){  
      UseCallback();
    }
     
    function GetRandomNumberFromServer(TextBox1, context){  
      document.forms[0].TextBox1.value = TextBox1;
    }
  </script>
   
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <input id="Button1"
         type="button"
         value="Get Random Number"
         onclick="GetNumber()" />
    <br />
    <br />
    <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
  </div>
  </form>
</body>
</html>
 
File: Default.aspx.cs
 
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 RandomNumber : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
  private string _callbackResult = null;
 
  protected void Page_Load(object sender, EventArgs e)
  {
    string cbReference = Page.ClientScript.GetCallbackEventReference(this,
       "arg", "GetRandomNumberFromServer", "context");
    string cbScript = "function UseCallback(arg, context)" +
       "{" + cbReference + ";" + "}";
 
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
       "UseCallback", cbScript, true);
  }
 
  public void RaiseCallbackEvent(string eventArg)
  {
    Random rnd = new Random();
    _callbackResult = rnd.Next().ToString();
  }
 
  public string GetCallbackResult()
  {
    return _callbackResult;
  }
}
Reena Jain replied to balaji mogadali on 02-Sep-11 03:00 AM
Hi,

The GetCallbackEventReference(Control, String, String, String) method performs an out-of-band callback to the server that is a modified version of a page's normal life cycle.
Here is example of use Callback random numeric value that will set one up. When a user clicks a button on the program will send the form values from the random to show in the TextBox.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="How-to-use-GetCallbackEventReference.aspx.vb" Inherits="How_to_use_Callback" %>
 
<!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>How to use GetCallbackEventReference</title>
    <script type="text/javascript">
      function GetNumber() {
        UseCallback();
      }
      function GetRandomNumberFromServer(txtGetNumber, context) {
        document.forms[0].txtGetNumber.value = txtGetNumber
      }
    </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <input id="Button1" type="button" value="Get Random Number" onclick="GetNumber()" /><br /><br />
    <asp:TextBox ID="txtGetNumber" runat="server"></asp:TextBox> </div>
  </form>
</body>
</html>

try this and let me know
Anoop S replied to balaji mogadali on 02-Sep-11 03:28 AM
GetCallbackEventReference->Obtains a reference to a client function that, when invoked, initiates a client call back to a server event. The client function for this overloaded method includes a specified control, argument, client script, and context.
http://msdn.microsoft.com/en-us/library/stax6fw9.aspx

You can use it for Server side call back

for eg:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="RandomNumber" %>

<!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 id="Head1" runat="server">
  <title>Callback Page</title>
  
  <script type="text/javascript">
    function GetNumber(){   
      UseCallback();
    }
    
    function GetRandomNumberFromServer(TextBox1, context){   
      document.forms[0].TextBox1.value = TextBox1;
    }
  </script>
  
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <input id="Button1" 
         type="button" 
         value="Get Random Number" 
         onclick="GetNumber()" />
    <br />
    <br />
    <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
  </div>
  </form>
</body>
</html>

File: Default.aspx.cs

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 RandomNumber : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
  private string _callbackResult = null;

  protected void Page_Load(object sender, EventArgs e)
  {
    string cbReference = Page.ClientScript.GetCallbackEventReference(this, 
       "arg""GetRandomNumberFromServer""context");
    string cbScript = "function UseCallback(arg, context)" +
       "{" + cbReference + ";" "}";

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
       "UseCallback", cbScript, true);
  }

  public void RaiseCallbackEvent(string eventArg)
  {
    Random rnd = new Random();
    _callbackResult = rnd.Next().ToString();
  }

  public string GetCallbackResult()
  {
    return _callbackResult;
  }
}

http://www.java2s.com/Tutorial/ASP.NET/0100__Page-Lifecycle/ServersidecallbackC.htm