Create Tooltip dynamically by AJAX PageMethod
Use Pop up div by JavaScript or AJAX Control Toolkit’s Hover Menu control can create a Tooltip easily.
However, if we need to create the content of tooltip dynamically by the Server-Side code, we can use the ICallbackEventHandler to achieve the faing goal, just like the solution in following link:
http://lancezhang.wordpress.com/2008/12/04/create-tooltip-dynamically
When you move the mouse on the text, the related image and text which will be created on the code-behind show up in the tooltip after 1 second. during the loading process, a “loading” gif picture will display in the tooltip box.
If we are using ASP.NET AJAX, the same effect can be achieve by AJAX Page Method easily.
Ok, here we go:
Step 1:
we need to set the EnablePageMethods=”true” for our ScriptManager or ToolkitScriptManager:
</asp:ScriptManager>
Step 2:
Let’s create the Server-side method for create tooltip message, please notice the using references and attribute:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
[ScriptService]
public partial class tooltiptest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetTooltip(string imagename)
{
System.Threading.Thread.Sleep(1000);
return @”this is just a demo: <br><img src=’” + imagename + “‘/>”;
}
}
Step 3:
Complete the JavaScript code to invoke the page method:
var positionX;
var positionY;
function onSuccess(value, ctx, methodName) {
document.getElementById(‘tooltipDiv’).innerHTML = value;
}
function onFailed(ex, ctx, methodName) {
alert(ex.get_exceptionType());
}
function HandleMouseOut() {
document.getElementById(‘tooltipDiv’).style.display = ‘none’;
document.getElementById(‘tooltipDiv’).innerHTML = “<img src=’loading.gif’/>”;
}
function HandleMouseOver(Label) {
PageMethods.GetTooltip(Label.innerText, onSuccess, onFailed);
positionX = event.clientX;
positionY = event.clientY;
document.getElementById(‘tooltipDiv’).style.left = positionX;
document.getElementById(‘tooltipDiv’).style.top = positionY;
document.getElementById(‘tooltipDiv’).style.display = ‘block’;
}
</script>
OK, the full code is following:
<!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 id=“Head1″ runat=“server”>
<script type=“text/javascript”>
var positionX;
var positionY;
function onSuccess(value, ctx, methodName) {
document.getElementById(‘tooltipDiv’).innerHTML = value;
}
function onFailed(ex, ctx, methodName) {
alert(ex.get_exceptionType());
}
function HandleMouseOut() {
document.getElementById(‘tooltipDiv’).style.display = ‘none’;
document.getElementById(‘tooltipDiv’).innerHTML = “<img src=’loading.gif’/>”;
}
function HandleMouseOver(Label) {
PageMethods.GetTooltip(Label.innerText, onSuccess, onFailed);
positionX = event.clientX;
positionY = event.clientY;
document.getElementById(‘tooltipDiv’).style.left = positionX;
document.getElementById(‘tooltipDiv’).style.top = positionY;
document.getElementById(‘tooltipDiv’).style.display = ‘block’;
}
</script>
<style type=“text/css”>
.style1
{
border: 1px solid #96C2F1;
background-color: #EFF7FF;
position: absolute;
display: none;
filter: alpha(opacity=80);
opacity: 0.80;
}
</style>
<title>Untitled Page</title>
</head>
<body style=“font-family: Calibri; font-size: 15px;”>
<form id=“form1″ runat=“server”>
<asp:ScriptManager ID=“ScriptManager1″ runat=“server” EnablePageMethods=“true”>
</asp:ScriptManager>
<div id=“tooltipDiv” class=“style1″>
<img src=“loading.gif” />
</div>
<div>
<asp:Label ID=“Label1″ runat=“server” Text=“http://forums.asp.net/Themes/FAN/style/i/logo.png”
onmouseout=“HandleMouseOut()” onmouseover=“HandleMouseOver(this)”></asp:Label>
<br />
<br />
<br />
<br />
<asp:Label ID=“Label2″ runat=“server” Text=“http://forums.asp.net/Themes/fan/images/roleicons/a874d69e-0ac8-4b80-acc7-512767e281f6.gif”
onmouseout=“HandleMouseOut()” onmouseover=“HandleMouseOver(this)”></asp:Label>
</div>
</form>
</body>
</html>
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
[ScriptService]
public partial class tooltiptest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetTooltip(string imagename)
{
System.Threading.Thread.Sleep(1000);
return @”this is just a demo: <br><img src=’” + imagename + “‘/>”;
}
}

[...] http://lancezhang.wordpress.com/2009/01/12/create-tooltip-dynamically-by-ajax-pagemethod/ [...]
Pingback by Create Tooltip dynamically by AJAX and WebService « LanceZhang’s Blog | January 12, 2009 |
Hi,
Good example, but for some reason I am getting System.InvalidOperationException in FF only in IE it works like a charm. This is frustrating that in doesn’t work in FF
Hi friend,
This code cannot work in Firefox, because it use the windows.event object.
In my another blog there is a solution for the tooltip in FF, I would like to suggest you have a try:
http://www.cnblogs.com/blodfox777/archive/2008/08/19/1271579.html
I had been facing this problem when I tried to return a DataTable. This error
was keeping on occuring what ever.
Here’s the solution.
Uncomment these lines in web.config.
Read More
http://muruganad.com/ASP.NET/ajax_PageMethods_System_InvalidOperationException.html
Thanks!
Murugan Andezuthu Dharmaratnam