Okay, I think I have this one figured out. It seems that some things changed
in the 2.0 DotNet framework with Beta 2. All of the "Client Callback"
samples I found on the web didn't seem to work. As a starting point, I ended
up using a bit of the code found in the article found here:
http://www.devx.com/dotnet/Article/20239/0/page/2. After trying a lot of
different things, I boiled it down to a simple example that I was able to
get to work. I'm using Visual Studio 2005 (Beta 2) with code-behind pages.
Here is the text from my "Test.aspx" page:
<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Test. aspx.cs"
Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Client Callback Test</title>
<script type="text/javascript">
function DoSomething(){
TheText =
document.getEle mentById("TextB ox1").getAttrib ute("value");
CallServer(TheT ext, "'DoSomethi ng' was called.");
}
function CallBackHandler (result,context )
{
document.getEle mentById("TextB ox2").innerTex t = "CallBackHandle r
Invoked. \nResult: " + result + "\nContext: " + context;
}
function ErrorCallBack(r esult,context)
{
alert("Error occurred : " + result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button"
onclick="DoSome thing()" />
<asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox><br /><br />
<asp:TextBox ID="TextBox2" runat="server" TextMode="Multi Line"
Height="140px" Width="312px"></asp:TextBox>
</div>
</form>
</body>
</html>
Here's the text from my "Test.aspx. cs" file:
using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
public partial class Test : System.Web.UI.P age, ICallbackEventH andler
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
string bScript = ClientScript.Ge tCallbackEventR eference(this,
"myArg", "CallBackHandle r", "myContext" , "ErrorCallBack" , true);
//create the Javascriptfunct ion that makes the actual server
call.
string sb = "function CallServer(myAr g,myContext)\n{ \n" +
bScript + "\n}";
//Register the clientscript.
Page.ClientScri pt.RegisterClie ntScriptBlock(t his.GetType(),
"CallServer ", sb.ToString(), true);
}
}
public String RaiseCallbackEv ent(String eventArgument)
{
int myInt = int.Parse(event Argument.Trim() ) * 2;
return "The new value is " + myInt.ToString( );
}
}
The first important thing to note is that the partial class inherits the
Web.UI.Page class AND implements the ICallbackEventH andler interface. I
think this takes care of the "...no suitable interface found..." type
errors. Then, it looks like the "GetCallbackEve ntReference" method has been
rolled into the ClientScript object, which is the "Page" implementation of
the "ClientScriptMa nager" class, so it must be called as such.
The basic explanation of how my example works is: When the user clicks on
"Button1," the javascript function "DoSomethin g" is called. The
"DoSomethin g" function grabs the value from "TextBox1" and passes it off in
the call to the server, by making a call to the "CallServer " function. The
actual javascript function "CallServer " will have already been created
during the initial Page_Load execution and inserted into the aspx page via
the "RegisterClient ScriptBlock" method (you'll note there is also some
additional auto-generated code created at this time that is inserted into
the client aspx page that is needed to do the callback). So, that
"CallServer " function on the client invokes the "RaiseCallbackE vent" method
on the server, which attempts to convert the string into an integer,
multiply it by two, and return the value as part of a new string. If the
initial value is an integer, it does the calculation and passes back the new
string back to the client, invoking the execution of the JavaScript
"CallBackHandle r" function, which displays the various lines of text, part
of which came from the server. If the initial value isn't an integer, the
server throws an exception and the client JavaScript function
"ErrorCallB ack" is executed instead. There is only one string that gets
passed from the client to the server (as a parameter in the
"RaiseCallbackE vent" method), and the second "Context" parameter is
something that stays unchanged (by the server) on the client side, which can
be useful to determine where in the code on the client side the callback was
made.
Well, anyway, hope this is helpful. I know I was glad to finally get this
much-celebrated, but as of yet, not terribly well-documented new feature to
work.
Troy
"Mike" <si***@newsgrou ps.nospam> wrote in message
news:98******** *************** ***********@mic rosoft.com...