1

I have an asp.net repeater that displays a title and and image. The title is very long , so I want to display the title again on mouse over.

I tried to implement a mouse over but I have the following problem.

My display looks like this :

  Repeater Element 1  Repeater Element 2
  Title 1             Title 2 
  Image 1             Image 2

Now on doing a mouse over on Element1 , my mouse over displays Title1. On doing a mouseover on Element2 , my mouse over displays Title1 again ,and I would like it to display Title2 ? Can anyone point me on how i can achieve this.

My code is below :

<asp:Repeater ID="rptMonitorSummary" runat="server" OnItemDataBound="rptMonitorSummary_OnItemDataBound">
                                            <ItemTemplate>
                                           <asp:Panel ID="Pnl" runat="server" onmouseover="return showsamplepopup();" onmouseout="return hidesamplepopup();">
                                                    <li class="ui-widget-content ui-corner-tr">
                                                        <h5 class="ui-widget-header">
                                                            <%# Eval("Name").ToString().Length > 9 ? (Eval("Name") as string).Substring(0, 9) : Eval("Name")%>
                                                        </h5>
                                                        <div id="popup" style="position: absolute; width: 80px; height: auto; background-color: Lime;
                                                            border-bottom: solid 3px gray; display: none; border-right: solid 3px gray; display: none;">
                                                            <%#Eval("Name")%>
                                                        </div>
                                                        <div class="center">
                                                            <asp:Image Width="50px" ID="btnPerformanceImage" runat="server" Height="28px"></asp:Image>
                                                        </div>
                                                    </li>
                                                </asp:Panel>
                                            </ItemTemplate>
                                        </asp:Repeater>

The javascript functions are as follows :

function hidesamplepopup() {
            document.getElementById('popup').style.display = 'none';
            return false;
        }
        function showsamplepopup(e) {
            e = (e) ? e : window.event;
            var element = (e.target) ? e.target : e.srcElement;
            var left = element.offsetLeft;
            var top = element.offsetTop;
            while (element = element.offsetParent) {
                left += element.offsetLeft;
                top += element.offsetTop;
            }
            document.getElementById('popup').style.display = 'block';
            document.getElementById('popup').style.left = left;
            document.getElementById('popup').style.top = top;
            return false;
        }
CodeNinja
  • 3,188
  • 19
  • 69
  • 112

3 Answers3

1

Notice that getElementById will return the first element with that ID. And you're using the same ID for your Div's.

You should either use a different ID for each item of the repeater (generating different ID's for each of them), or change your logic to fetch them by some other property. I highly recommend using jQuery as well.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
1

I do not know what is your requirement. It could have been a lot easier if you use jQuery tooltip.

This is just an alternative approach.

enter image description here

<link rel="stylesheet" 
 href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
    $(function () {
        $(document).tooltip();
    });
</script>

<asp:Repeater ID="rptMonitorSummary" runat="server" 
    OnItemDataBound="rptMonitorSummary_OnItemDataBound">
    <ItemTemplate>
        <asp:Panel ID="Pnl" runat="server">
            <li class="ui-widget-content ui-corner-tr">
           <h5 class="ui-widget-header" title="<%# Eval("Name").ToString() %>">
                    <%# Eval("Name").ToString().Length > 9 ? 
                   (Eval("Name").ToString()).Substring(0, 9) : Eval("Name")%>
                </h5>
                <div class="center">
                    <asp:Image Width="50px" ID="btnPerformanceImage" 
                      runat="server" Height="28px"></asp:Image>
                </div>
            </li>
        </asp:Panel>
    </ItemTemplate>
</asp:Repeater>
Win
  • 61,100
  • 13
  • 102
  • 181
  • +1 Wow! this is totally new for me & it works great ! But I have a couple of questions .1) The statement actually changes the entire theme of my page and without the statement the popup appears as a huge rectangle spanning from one end of the screen to the other. Is there a work around for this and 2) is there a way I can include more elements of the repeater within the tool tip ? – CodeNinja May 20 '13 at 22:43
  • **1)** You can configure CSS based on what you need here - http://jqueryui.com/tooltip/ **2)** Yes you can have multiple `Eval` like this `
    ">...
    `.
    – Win May 21 '13 at 04:01
  • Thank you ! The link you showed me helped me almost in accomplishing my task. I have css problems now . on setting width:auto; the tool tip shows up as a huge rectangle from one side of the screen to another. I tried setting up the width : to a fixed value like 200px , but since the text within the tool tip changes for every repeater . The fixed width doesnt seem to look good on the screen. – CodeNinja May 21 '13 at 17:40
  • http://stackoverflow.com/a/12957638/296861 If you have more question about CSS and jQuery Tooltip, please create a new question so it'll help others too? – Win May 21 '13 at 17:51
  • Hey I just posted another question regarding this. I would like to use the tool tip to display info in different lines : http://stackoverflow.com/questions/16718463/show-details-for-asp-net-repeater-using-jquery-javascript-tool-tip – CodeNinja May 23 '13 at 17:23
  • I answered your question. Take a look at it and let me know. http://stackoverflow.com/a/16720686/296861 – Win May 23 '13 at 18:24
0

I would change the way which you are binding the events to elements, as your sample code doesn't use jQuery I'll assume you don't want it :)

First things first, you'll want to add a class to the asp:panel so that there will be some way of identifying and selecting all the instances. Also you'll want to use classes for your popups not IDs as IDs should be unique on a page.

then you can do something like:

var elements = document.querySelectorAll('.thatClassYouAdded'),
i = 0, l = elements.length;

for(;i<l;i++)
{
    elements[i].addEventListener('mouseover', function(e) {
        var popup = this.querySelector('.popup');
        //do some stuff to popup
    });
}

It's also important to note that querySelector is not supported in legacy browsers (see https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector for more info and support table) and older IEs (pre 9) use attachEvent instead of addEventListener which you may need to write additional code to support

Matt Berkowitz
  • 975
  • 6
  • 13