Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 17th, 2009, 04:53 AM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: God's country , United Kingdom.
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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

Last edited by elwappo; November 17th, 2009 at 04:57 AM..
  #2 (permalink)  
Old November 17th, 2009, 06:02 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old November 17th, 2009, 06:55 AM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: God's country , United Kingdom.
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
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!
  #4 (permalink)  
Old November 17th, 2009, 07:27 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old November 17th, 2009, 07:43 AM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: God's country , United Kingdom.
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
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
  #6 (permalink)  
Old November 17th, 2009, 08:21 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #7 (permalink)  
Old November 20th, 2009, 06:01 AM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: God's country , United Kingdom.
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
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!
  #8 (permalink)  
Old November 20th, 2009, 06:16 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
elwappo (November 20th, 2009)
  #9 (permalink)  
Old November 20th, 2009, 06:33 AM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: God's country , United Kingdom.
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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?
  #10 (permalink)  
Old November 20th, 2009, 06:46 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ICallbackEventHandler Interface ilegend .NET Framework 2.0 2 August 25th, 2008 10:56 PM
ICallbackEventHandler.GetCallbackResult Problem tropicalpisces ASP.NET 2.0 Professional 0 August 11th, 2008 01:04 PM



All times are GMT -4. The time now is 06:43 PM.


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