|
|
 |
| 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 computer programmers 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 programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
|
 |
|

November 17th, 2009, 04:53 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Location: Swansea, , United Kingdom.
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|

November 17th, 2009, 06:02 AM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
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
|

November 17th, 2009, 06:55 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Location: Swansea, , United Kingdom.
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by 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!
|

November 17th, 2009, 07:27 AM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
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
|

November 17th, 2009, 07:43 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Location: Swansea, , United Kingdom.
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
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
|

November 17th, 2009, 08:21 AM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
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
|

November 20th, 2009, 06:01 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Location: Swansea, , United Kingdom.
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
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!
|

November 20th, 2009, 06:16 AM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

November 20th, 2009, 06:33 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Location: Swansea, , United Kingdom.
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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?
|

November 20th, 2009, 06:46 AM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |