p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Professionals (http://p2p.wrox.com/forumdisplay.php?f=352)
-   -   ICallbackEventHandler.RaiseCallbackEvent (http://p2p.wrox.com/showthread.php?t=77058)

elwappo November 17th, 2009 04:53 AM

ICallbackEventHandler.RaiseCallbackEvent
 
Hi all,
I've been looking into the MSDN exampe of how to implement a simple client/server callback, which involves implementing the ICallbackEventHandler interface, which works fine. http://msdn.microsoft.com/en-us/library/ms178210(VS.80).aspx

The example shows that you can pass an extra context value with the partial postback, and you can also pick it up on the return client callback function. Yup, understood and I can see how that works.

However, the implementation of "RaiseCallbackEvent" function on the server side accepts only one parameter and there seems no way of accessing the context value in the code behind, and have the ability to manipulate if needed?

Here's the example I've been looking at:

1. A button fires a javascript function:
<button type="button" onclick="LookUpStock()">Look Up Stock</button>

2. The javascript function:
<script type="text/ecmascript">
function LookUpStock()
{
var lb = document.getElementById("ListBox1");
var product = lb.options[lb.selectedIndex].text;
CallServer(product, "mycontextval");
}

function ReceiveServerData(rValue,contextvalue)
{
document.getElementById("ResultsSpan").innerHTML = rValue + ' ' + contextvalue;
}
</script>

3. The Call Server Javascript call which fires the postback and contains the parameters and return client callback function:
<script type="text/javascript">
//<![CDATA[
function CallServer(arg, context){ WebForm_DoCallback('__Page',arg,ReceiveServerData, context,null,false);}//]]>
</script>

You can see the "arg" AND "context" values in the WebForm_DoCallback function, but you cannot seem to access it on the server as the RaiseCallbackEvent event signature has only one string parameter, and handles the in coming "arg" value only:

void ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)
{
// code here
}

Does anyone know how you can access the context value in the code behind? As it seems to be sent to the server. I can see how everything hangs together, and how the calls are working, just can't work out how to to access the context value server side!

Any help would be appreciated...
Regards
Elwappo

Imar November 17th, 2009 06:02 AM

Hi there,

As far as I know, the context parameter is purely client side and not available on the server.

You may want to take a look at this article for more details: http://www.west-wind.com/Weblog/posts/2302.aspx

Cheers,

Imar

elwappo November 17th, 2009 06:55 AM

Quote:

Originally Posted by Imar (Post 249577)
Hi there,

As far as I know, the context parameter is purely client side and not available on the server.

You may want to take a look at this article for more details: http://www.west-wind.com/Weblog/posts/2302.aspx

Cheers,

Imar

Thank you, I shall take a look at that article. What I find strange is that the value is sent to the server, but is no way accessible!

Imar November 17th, 2009 07:27 AM

I don't think it's sent to the server. It's sent to WebForm_DoCallback where it's stored in the "pending callbacks" so it can be provided again when the callback returns.

I took your example, turned on Fiddler and never saw the context value getting passed over the wire.

I am not 100% sure, but I think the context is a 100% client side context object.

Imar

elwappo November 17th, 2009 07:43 AM

Quote:

Originally Posted by Imar (Post 249596)
I don't think it's sent to the server. It's sent to WebForm_DoCallback where it's stored in the "pending callbacks" so it can be provided again when the callback returns.

I took your example, turned on Fiddler and never saw the context value getting passed over the wire.

I am not 100% sure, but I think the context is a 100% client side context object.

Imar

Ah I see. Thank you very much for taking a look at it. Much appreciated. Looks like a comma separated value will need to be sent if I want to append more parameters.
Regards

Imar November 17th, 2009 08:21 AM

Yeah, that would work. Or you can use PageMethods that allow you to pass back and forth complex objects. Here's a simple example that asks a user for a product ID, creates a Product instance (which is defined in the Code Behind class) and passes it to AddToOrder (which is defined as a static method in the Code Behind and has the WebMethod attribute applied.) Note: you also need a ScriptManager with EnablePageMethods set to true:

Default.aspx
Code:

<%@ 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></title>
 <script type="text/javascript">
  function AddProductToOrder(id)
  {
  var myProduct = new Product();
  myProduct.Id = id;
  myProduct.Name = "Some Value";
  PageMethods.AddToOrder(myProduct, SuccessCallback);
  }
  function SuccessCallback(result)
  {
  alert('Order ID is ' + result);
  }
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
  </asp:ScriptManager>
  <input onclick="AddProductToOrder(prompt('Id'));" id="Button1" type="button" value="Add to order" />
 </div>
 </form>
</body>
</html>

Default.aspx.cs
Code:

using System;
using System.Web.Services;
public class Product
{
 public string Id { get; set; }
 public string Name { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
 [WebMethod]
 public static string AddToOrder(Product product)
 {
  // TODO add logic here. Right now, for this example
  // I simply return the product Id and some constant value
  return "Test " + product.Id;
 }
}

Hope this helps,

Imar

elwappo November 20th, 2009 06:01 AM

Quote:

Originally Posted by Imar (Post 249606)
Yeah, that would work. Or you can use PageMethods that allow you to pass back and forth complex objects. Here's a simple example that asks a user for a product ID, creates a Product instance (which is defined in the Code Behind class) and passes it to AddToOrder (which is defined as a static method in the Code Behind and has the WebMethod attribute applied.) Note: you also need a ScriptManager with EnablePageMethods set to true:

Default.aspx
Code:

<%@ 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></title>
 <script type="text/javascript">
  function AddProductToOrder(id)
  {
  var myProduct = new Product();
  myProduct.Id = id;
  myProduct.Name = "Some Value";
  PageMethods.AddToOrder(myProduct, SuccessCallback);
  }
  function SuccessCallback(result)
  {
  alert('Order ID is ' + result);
  }
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
  </asp:ScriptManager>
  <input onclick="AddProductToOrder(prompt('Id'));" id="Button1" type="button" value="Add to order" />
 </div>
 </form>
</body>
</html>

Default.aspx.cs
Code:

using System;
using System.Web.Services;
public class Product
{
 public string Id { get; set; }
 public string Name { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
 [WebMethod]
 public static string AddToOrder(Product product)
 {
  // TODO add logic here. Right now, for this example
  // I simply return the product Id and some constant value
  return "Test " + product.Id;
 }
}

Hope this helps,

Imar

Thanks for that Imar - I've used a web method with the autocomplete extender, which is what got me thinking about the context key.

One thing that I noticed with the autocomplete extender is that the web method you declare must have the signature (string prefixText, string contextKey) which means you can clearly access the context value server side.

This is what confused me and brought about my initial post!

Imar November 20th, 2009 06:16 AM

I see; thanks for the follow up.

When posting replies, can you please not quote the entire message? Makes it difficult to read this thread; the original post is always close to the replies, so there's no real need for it anyway....

Imar

elwappo November 20th, 2009 06:33 AM

No worries. So is there any reason why they would fix the name of the parameters of the web method specifically to prefixText and contextKey on the autocomplete extender? Because surely you can call you parameters on the server side method anything you want?

Imar November 20th, 2009 06:46 AM

I don't know. Indeed, any name would work. Personally, I think context makes more sense than contextKey as the parameter holds the actual context, not just some key pointing to it.....

http://www.asp.net/AJAX/AjaxControlT...oComplete.aspx

Imar


All times are GMT -4. The time now is 01:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.