error using ICallbackEventHandler in ASP.Net 2.0

G

Guest

When trying to compile (using Visual Web Developer 2005 Express Beta;
frameworkv2.0.50215 ) the source code below I get errors (listed below due to
the use of ICallBackEventHandler. Ultimately I want to use a callback from
the client side to update webcontrols based on user input without using
postback.

I am seeking a way to stop the compile errors.

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 : ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{

}

public string RaiseCallbackEvent(string eventArgument) { }
}

I get these errors
1. 'ASP.Index_aspx.FrameworkInitialize()': no suitable method found to
override c:\WINNT\Microsoft.NET\Framework\v2.0.50215\Temporary ASP.NET
Files\callback\02ec0c94\71251b27\tvquf1zy.0.cs

2. 'ASP.Index_aspx.GetTypeHashCode()': no suitable method found to
override c:\WINNT\Microsoft.NET\Framework\v2.0.50215\Temporary ASP.NET
Files\callback\02ec0c94\71251b27\tvquf1zy.0.cs

I added the code
protected virtual void FrameworkInitialize()
{

}

protected override void GetTypeHashCode()
{

}

but then I get an error about the access level of GetTypeHashCode
Error 1 'ASP.Default_aspx.GetTypeHashCode()': cannot change access modifiers
when overriding 'protected' inherited member
'_Default.GetTypeHashCode()' c:\WINNT\Microsoft.NET\Framework\v2.0.50215\Temporary ASP.NET Files\callback\02ec0c94\71251b27\zqawgoe7.0.cs
 
T

Tman

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.getElementById("TextBox1").getAttribute("value");
CallServer(TheText, "'DoSomething' was called.");
}

function CallBackHandler(result,context)
{
document.getElementById("TextBox2").innerText = "CallBackHandler
Invoked. \nResult: " + result + "\nContext:" + context;

}
function ErrorCallBack(result,context)
{
alert("Error occurred : " + result);
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button"
onclick="DoSomething()" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"
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.Configuration;
using System.Collections;
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 Test : System.Web.UI.Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
string bScript = ClientScript.GetCallbackEventReference(this,
"myArg", "CallBackHandler", "myContext", "ErrorCallBack", true);
//create the Javascriptfunction that makes the actual server
call.
string sb = "function CallServer(myArg,myContext)\n{\n" +
bScript + "\n}";
//Register the clientscript.
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", sb.ToString(), true);
}

}
public String RaiseCallbackEvent(String eventArgument)
{
int myInt = int.Parse(eventArgument.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 ICallbackEventHandler interface. I
think this takes care of the "...no suitable interface found..." type
errors. Then, it looks like the "GetCallbackEventReference" method has been
rolled into the ClientScript object, which is the "Page" implementation of
the "ClientScriptManager" 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 "DoSomething" is called. The
"DoSomething" 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 "RegisterClientScriptBlock" 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 "RaiseCallbackEvent" 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
"CallBackHandler" 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
"ErrorCallBack" is executed instead. There is only one string that gets
passed from the client to the server (as a parameter in the
"RaiseCallbackEvent" 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

news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top