Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - tooltip.gif

Introduction

There are lot of examples and articles on tooltip but I didn't find any which displays the content from a database table dynamically. I have gone through different articles and at last used the following approach which I want to share with you all.

This article describes how to create a dynamic tooltip in ASP.NET using JavaScript. Here, we can design our tooltip as we want its look, it's not like conventional Windows tooltip. If we want, we can insert images also to our tooltip. The appearance of the tooptip can be changed according to requirement. This article contains very simple code of JavaScript and ASP.NET.

Implementation

I'll try to explain this article using an example. Let us take a database table having the following fields:

Here, we are binding this table to a DataGrid and we are displaying only few fields (Client_id, Client_Name and the Status of the client). Now, our requirement is when the user places his mouse over a particular row of the DataGrid, we need to display the Client_name, Address, city, state, Phone and Fax of the client using a tooltip.

First, we'll design our tooltip. It'll be better if we'll use a DIV tag so that we can move this tag anywhere in our document, and whenever required, we can hide it on mouseout event. Within the DIV tag here, I am using a table, and each cell of the table is dynamically filled at mouseover event using JavaScript.

<div id="Popup" class="transparent">
  <div style="background-color: #003366"><b><center>
                 <span>Address</span></center></b></div>
   <div>
   <table width=100% border=0 cellpadding=0 cellspacing=0>
    <tr>
     <td id=td0 align=left class=runtext1></td>
    </tr>
    <tr>
     <td id=td1 align=left class=runtext></td>
    </tr>
    <tr>
     <td id=td2 align=left class=runtext></td>
    </tr>
    <tr>
     <td id=td3 align=left class=runtext></td>
    </tr>
    <tr>
     <td id=td4 align=left class=runtext></td>
    </tr>
    <tr>
     <td id=td5 align=left class=runtext></td>
    </tr>
   </table>
   
   </div>
 </div>

Here for each cell of the table, the text we'll fill dynamically using innerText property of the table cell. This we'll see in our JavaScript function.

Next step is to write the JavaScript function which will fill the table cells dynamically.

<script language="javascript">
function ShowTooltip(name,address,city,state,phone1,fax)
{
    document.getElementById("td0").innerText=name; 
    document.getElementById("td1").innerText=address;
    document.getElementById("td2").innerText=city;
    document.getElementById("td3").innerText=state;
    document.getElementById("td4").innerText=phone1;
    document.getElementById("td5").innerText=fax;
    x = event.clientX + document.body.scrollLeft;
    y = event.clientY + document.body.scrollTop + 10;
    Popup.style.display="block";
    Popup.style.le ft = x;
    Popup.style.top = y;
 }

function HideTooltip()
{
    Popup.style.display="none";
}
</script>

Here in ShowTooltip function, we are getting 6 arguments and setting these values to the cells of the table. After this, we are getting the current mouse position x-y coordinates and displaying the tooltip at desired location. I think hide function is understood; it is fired at mouseout event of the DataGrid row.

Now, our final and most important task is to pass the values from the database table to the JavaScript function at the time of DataGrid binding, and add the mouseover and mouseout events to each DataGrid row. For this, we'll use the DataGrid's ItemDataBound event, which occurs after an item is data bound to the DataGrid control. This event provides us with the last opportunity to access the data item before it is displayed on the client. After this event is raised, the data item is nulled out and no longer available.

//[C#]


void dgClient_ItemDataBound(object sender, 
             DataGridItemEventArgs e)
{
  if (e.Item.DataItem != null)
  {
    e.Item.Attributes.Add("onmouseover", "ShowTooltip('" + 
      DataBinder.Eval(e.Item.DataItem, "client_name").ToString() + "','" + 
      DataBinder.Eval(e.Item.DataItem, "Address").ToString() + "','" + 
      DataBinder.Eval(e.Item.DataItem, "CITY").ToString() + "','" + 
      DataBinder.Eval(e.Item.DataItem, "STATE").ToString() + "', '" + 
      DataBinder.Eval(e.Item.DataItem, "PHONE1").ToString() + "','" + 
      DataBinder.Eval(e.Item.DataItem, "FAX").ToString() + 
      "');"); 
   e.Item.Attributes.Add("onmouseout","HideTooltip();");
  }
}

Conclusion:

I am also new to ASP.NET, there may be some better way to do this also. If you are having any suggestion or comments, please do tell me, it will really help me to improve myself. I hope the rest of the code is not required for this simple example, and I don't want to waste the precious CP space. If the complete code is required, just tell me, I'll send the code.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalin c#
mrprata
21:31 21 Jul '09  
hi there. do u have the codes in c#? i just need a tooltip which can display the details in a database.just a simple text and image.

in my database there will only be 2 things
- event name
- image

thats all. and will display these two things in the tooltip when someone roll over at a place.
if possible, can send the codes to my another email:

anarchy_protest@hotmail.com
GeneralIt doen's work in Mozilla
dipudo
23:35 29 Apr '08  
It works fine in IE. but when i check this is shows nothing.
If you have any suggestion for mozilla then please mail me at

nikita.prajapati@bytetechnosys.com
Generalhow to Load images
ajay_manral@rediffmail.com
22:39 19 Oct '07  
Cool
Hi, I have a problem when I wanted load an image the DIV table just display the path, can you help me with this?
may be if you send me a little code
Thanks
Ajay


ajay
GeneralThanks
naveensky
6:24 27 Sep '07  
Thanks a lot buddy for this piece of code !! this really helped me a lot !!
QuestionLoad Image
wrivera
13:09 8 Jun '07  
Hi, I have a problem when I wanted load an image the DIV table just display the path, can you help me with this?
may be if you send me a little code
Thanks
Waldo
Generalcode request please
dagma20
7:21 16 May '07  
Please can I have a copy of the code please:

goodboystewboy@hotmail.com Suspicious
GeneralAn Easier way
Gibstov
10:47 8 Feb '07  
If all you want is the standard tooltip, here is an easier way. No need of javascript or adding any extra tags to you aspx page.

In the ItemDataBound Event do this:


Private Sub dgContacts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgContacts.ItemDataBound

If Not e.Item.DataItem Is Nothing Then

e.Item.ToolTip = DataBinder.Eval(e.Item.DataItem, "vchrAltPhoneDescription")
End If

End Sub


This assumes the tool tip is in vchrAltPhoneDescription, replace this with the column that hold the tool tip text.

Questiontable style
MaxAtChamber
17:48 18 Jan '07  
It's very useful,

Can you send me the DIV transparent style to me

wwwdotmax@yahoo.co.uk

Many Thanks! Smile
Questioni need your help
anil 1kumar
23:24 28 Nov '06  
hello dear
i manil from mumabi, i m intermidiate user of .net.
now i i m able to add textbox control in datarid.. so my quetion statrs from here?
1) how can valiate the editable datagrid cells?
explianing you my quetion::: if i m updating the data at that time we should validate that textbox control?
how is it works?
please reply me
thnks in advance..
anil sahu

we

QuestionI need your Help
CCs_Vzla_Asp
4:03 29 Sep '06  
Hi Prakash

I test your code, and i can say it's excellet.

But, i can't to use in ASP.net

I don't know why, but, the mistake it's when i call the javascript

I use this code:

e.Item.Attributes.Add("onmouseover", "javascript:ShowTooltip(DataBinder.Eval(e.Item.DataItem,Codigo));")

The error is: DataBinder is not declared

Can you help me??

I try to change the code, but always obtain a mistakes in a debbugin

Excuseme for my english but i only speak spanish.

Regards from Venezuela Roll eyes

Confused Confused Confused Confused Confused Confused Confused



GeneralSource code
leosong
7:54 17 Sep '06  
Hi Prakash,

I need the source code for this project. Can you please send it to me at songleo@hotmail.com ?

Thanks.
Leo


songleo

GeneralRe: Source code
Prakash Kalakoti
19:19 17 Sep '06  
Hi, source code is now attached with this article. You can find the download link at the top of the article.
GeneralI would like to have the source code as well
Sue_NC
6:31 13 Sep '06  
this is cool! I would like to have the source code as well Smile

sue AT edream DOT org

Thanks!!!
Sue
http://www.edream.org

Sue's edream
.NET, AJAX, Flash oh My!
http://www.edream.org

GeneralSource Code
Stephen Noronha
8:13 1 Sep '06  

Prakash,

Neat article. Can you send me the source code Please? I think you should publish your source code... Smile there are a number of requests..

My email id: stephen_jn@hotmail.com


Thanks,
Stephen
Generalsource code please..
nmarun
9:17 25 Aug '06  
Hi Prakash,

I need the source code for this project.. can you please send it to me: nmarun@rediffmail.com

Thanks
Arun
GeneralSource Code
luke68
9:13 7 Aug '06  
Can you please send me the source code? Thanks!

luke68

GeneralStyle Sheet?
mikemachos
10:22 26 Jul '06  
I'd just like to say thanks for the code..it works great.

Do you have the style transparent for this.

muster765@yahoo.com

Thanks
Mike
GeneralReally simple and excellent
Bala-vikash
22:08 25 Jul '06  
Yaa cool.. man. Smile
i apply this code in my page. but Tooltip is not moving.

Please send me the code.

my id is : sri_balasubramanian@yahoo.com Smile

Bala
GeneralRe: Really simple and excellent
Prakash Kalakoti
18:50 26 Jul '06  
Done
GeneralSorce Code
Prakash Kalakoti
4:35 24 Jul '06  
Hi,

Those who want the source code before I update my article, please provide me your mail id so that i'll send you the source code.
GeneralRe: Sorce Code
cs231
18:11 26 Jul '06  
Prakash,

Could you please send me the source code. I am having problem with the DIV. so hope it will be solved if I can look at the source code.

Thank you,
Chandu
GeneralRe: Sorce Code
cs231
18:12 26 Jul '06  
my email is chandu231@yahoo.com

Thanks,
Chandu
GeneralRe: Sorce Code
Prakash Kalakoti
18:47 26 Jul '06  
Done Smile
GeneralRe: Sorce Code
asifrazach
8:57 4 Aug '06  
Cna You Please Sned Me The Code at
5177637@gmail.com

Thanks
QuestionRe: Sorce Code
A.M.Sivakumaran
0:51 16 Aug '06  

Hi Prakash

This stuff is amazing, it would be great if you can share the source code.

This is my email id.shivan4u@gmail.com

Cheers
Shiva


Last Updated 14 Sep 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010