ClientCallbacks - is there any way to know the context in the RaiseCallbackEvent method?

Last post 02-14-2006 10:19 PM by Barry Tang. 1 replies.

Sort Posts:

  • ClientCallbacks - is there any way to know the context in the RaiseCallbackEvent method?

    01-26-2006, 7:33 PM
    • Participant
      765 point Participant
    • Edgardo
    • Member since 04-28-2005, 4:21 PM
    • Buenos Aires, Argentina
    • Posts 153
    This is really weird, why even though we pass a context variable in the JavaScript there is no "context" parameter in the RaiseCallbackEvent method?

    I'm trying to use callbacks with more than just one control in the same page, so how can I know who invoked the callback (and return the appropiate results for each control)?

    Is there any way or do I have to pass the eventArgument and the context in the same parameter?
  • Re: ClientCallbacks - is there any way to know the context in the RaiseCallbackEvent method?

    02-14-2006, 10:19 PM
    • Member
      418 point Member
    • Barry Tang
    • Member since 10-31-2002, 11:49 AM
    • Posts 87
    • AspNetTeam
    Hi,

    Here is a definition for the context:

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

    Here is an example on how to use it:

    In Default.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!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>Untitled Page</title>

    <script type="text/javascript">

    function ReceiveServerData(arg, context)

    {

    Arg1.innerText = "arg = " + arg;

    Arg2.innerText = "context = " + context;

    }

    function context(value)

    {

    return "client " + value;

    }

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <input type="button" value="CallBack"

    onclick="CallServer('input1', context('input1'))"/>

    <br />

    <span id="Arg1"></span>

    <br />

    <span id="Arg2"></span>

    </div>

    </form>

    </body>

    </html>

    and in Default.aspx.cs

    using System;

    using System.Data;

    using System.Configuration;

    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 _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler

    {

    private string aStringValue;

    protected void Page_Load(object sender, EventArgs e)

    {

    ClientScriptManager cm = Page.ClientScript;

    String cbReference = cm.GetCallbackEventReference(this, "arg",

    "ReceiveServerData", "context");

    String callbackScript = "function CallServer(arg, context) {" +

    cbReference + "; }";

    cm.RegisterClientScriptBlock(this.GetType(),

    "CallServer", callbackScript, true);

    }

    #region ICallbackEventHandler Members

    public string GetCallbackResult()

    {

    return aStringValue;

    }

    public void RaiseCallbackEvent(string eventArgument)

    {

    aStringValue = "server " + eventArgument;

    }

    #endregion

    }

    Note the main point here is context(value) get called on the client side and the result is passed back to the client-side event handler ReceiveServerData(arg, context).

    I hope this helps,

    Barry

     

    ------------------------------------------------------------

    This posting is provided "AS IS" with no warranties, and confers no rights.
Page 1 of 1 (2 items)