I would greatly appreciate some guidance on asp.net and javascript

I am just starting out with JavaScript and facing some challenges.

Currently, I'm struggling to develop a Mouseover and mouseout script.

Here is my ASPX code:

<div>
    <div id="div_img" style="height: 300px; width: 300px; border: solid 1px black;   position: absolute;
            visibility: hidden;">
        <img src="" id="img_tool" />
    </div>
</div>

<script type="text/javascript" language="javascript">

        function ShowToolTip(con) {
        console.log(getOffset(con).left + '-' + getOffset(con).top);
            document.getElementById("div_img").style.visibility = "visible";
            document.getElementById("img_tool").src = con.src;
            document.getElementById("div_img").style.left = getOffset(con).left - 300 + 'px';
            document.getElementById("div_img").style.top = getOffset(con).top - 300 + 'px';
            document.getElementById("div_img").style.zIndex = "0";
            console.log(document.getElementById("div_img").style.left +'-'+document.getElementById("div_img").style.top)
        }
        function hideToolTip() {
            document.getElementById("div_img").style.visibility = "hidden";
        }

        function getOffset( el ) {
            var _x = 0;
            var _y = 0;
            while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
                _x += el.offsetLeft - el.scrollLeft;
                _y += el.offsetTop - el.scrollTop;
                el = el.offsetParent;
            }
            return { top: _y, left: _x };
        }

</script>

As for my C# code behind:

 if ((e.Row.RowType.ToString() != "Header") && (e.Row.RowType.ToString() != "Footer"))
 {
      ImageButton ib = (ImageButton)e.Row.Cells[3].Controls[0];
      ib.Attributes.Add("onmouseover", "ShowToolTip(this);");
      ib.Attributes.Add("onmouseout", "hideToolTip();");

I would greatly appreciate any guidance on what steps to take next. Thank you in advance.

Answer №1

Adding events directly to the img_tool in aspx can be achieved by:

<img src="" id="img_tool" onmouseover="ShowToolTip(this);" onmouseout="hideToolTip();" />

To access your controls (web controls or html elements) in code behind, don't forget to include the attribute runat="server":

<img src="" id="img_tool" runat="server"/>

Answer №2

here is a suggestion

if ((e.Row.RowType.ToString() != "Header") && (e.Row.RowType.ToString() != "Footer"))
  {
  ImageButton ib = (ImageButton)e.Row.Cells[3].Controls[0];
  ib.Attributes.Add("onmouseover", string.Format("ShowToolTip('{0}');", ib.ClientId));
  ib.Attributes.Add("onmouseout", string.Format("hideToolTip('{0}');", ib.ClientId));
  }


    <script type="text/javascript" language="javascript">

  function ShowToolTip(con) {
    var element = document.getElementById(con);
    console.log(getOffset(con).left + '-' + getOffset(con).top);
    document.getElementById("div_img").style.visibility = "visible";
    document.getElementById("img_tool").src = element.src;
    document.getElementById("div_img").style.left = getOffset(con).left - 300 + 'px';
    document.getElementById("div_img").style.top = getOffset(con).top - 300 + 'px';
    document.getElementById("div_img").style.zIndex = "0";
    console.log(document.getElementById("div_img").style.left + '-' + document.getElementById("div_img").style.top)
  }
  function hideToolTip() {
    document.getElementById("div_img").style.visibility = "hidden';
  }

  function locatePosition(el) {
    var _x = 0;
    var _y = 0;
    var element = document.getElementById(el);
    while (element && !isNaN(element.offsetLeft) && !isNaN(element.offsetTop)) {
      _x += element.offsetLeft - element.scrollLeft;
      _y += element.offsetTop - element.scrollTop;
      element = element.offsetParent;
    }
    return { top: _y, left: _x };
  }

</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to dynamically change HTML content by utilizing a JSON file?

Looking to implement a JavaScript loop (using jQuery) that can dynamically populate the HTML file with content from a JSON file based on matching <div> ids to the JSON "id" values. The solution should be scalable and able to handle any number of < ...

width of the cells within the grid table

What is the best way to ensure the width alignment of cells in both the header and main section? I have highlighted the correct option in the image with green checkmarks. Check out the image here. Here is my example and solution: View the code on CodePen. ...

Can .map() be utilized to transform an array of objects into a single object?

Presented here is a subset of data extracted from a larger dataset in a project: const data = [ { scenario: '328', buckets: 2 }, { scenario: '746', buckets: 1 }, { scenario: '465&apos ...

What is the best way to obtain the value of the currently selected option in a bootstrap 5 multi-select dropdown menu?

After some attempts, I realized that what I was retrieving were just the list values of select options instead of the specific option that triggered the onchange event. Is there a way for me to accurately determine the value of the selected option that c ...

Guide to setting up Date Range Validator within MVC 4

Is there a way to limit the user from inputting a date outside of a specific range in my MVC 4 application? I'd appreciate any advice on how to achieve this. ...

Explore innovative circular navigation with the wheelnav.js library - where SVG elements can dynamically change position but keep

I recently started exploring wheelnav.js for creating a unique navigation system. The Spreader feature caught my attention, particularly the 2nd example. However, when I implemented it, the text did not rotate as expected. I followed the code example pre ...

Can a file be transferred from an Electron application to an Express server by supplying the file path?

This is my current objective: A user drags and drops a file into Electron Electron uses a python script via child_process.exec to convert the file The conversion process generates a new file in the same directory as the original With knowledge of the path ...

conceal the .card-body element if the children have the CSS property "display:none"

My challenge involves managing two collapsible cards on a webpage. I am looking for a solution where the .card-body will have its display set to none when there are no inner divs to show in the card upon clicking a letter in the pagination. Otherwise, the ...

Is there a way to incorporate a computed value into a table prop while working with Element UI?

In my frontend development, I am utilizing vuejs along with element ui. I am faced with the task of rendering a table that includes dates in unix format. To make these dates more user-friendly, I have incorporated moment.js to convert them into a readable ...

Is it possible to utilize SCOPE_IDENTITY when executing a multiple insert statement?

When importing numerous data rows from a csv file into a SQL Server database via a web application, it is essential to obtain the auto-generated id values for the client. However, using a loop for this process leads to poor performance, despite being able ...

If the item with the specified name is not found in the list, display an image using Vue's v-if directive

<ul class="ulItems" v-for="item in listingItems" :key="item.channels"> <li class="liItems"> {{ item.itemName }} </li> </ul> I am looking to incorporate an image in situations where th ...

How to efficiently convert XML data and transform it into a flattened JSON format

My XML file has the following structure: <root> <foos> <foo> <id>1</id> <number>1245</number> </foo> <foo> <id>2</id> ...

How to get the most out of the $scope variable?

Is it possible to assign a regular JavaScript variable the current value of an Angular $scope variable without having their values binded together? //$scope.var1 is set to a specific value, for example, 5 var v2 = $scope.var1; //$scope.var1 is then update ...

Passing ngModel from controller to directive in AngularJS

I'm currently working on a project that involves a controller with an attribute directive nested inside of it. This directive requires access to the ngModel of its parent controller. For more context, feel free to check out this Plunkr. Issue at Han ...

Navigating a intricate JSON structure using JavaScript - the ultimate guide!

I am facing a challenge with a complex JSON object that I need to traverse and add additional properties to. Below is an example of the JSON structure: Object {root: Object} root: Object entity_children: Array[1] 0: Object ...

Using Javascript to Treat an HTML Document as a String

Check out the complete project on GitHub: https://github.com/sosophia10/TimeCapsule (refer to js/experience.js) I'm utilizing JQuery to develop "applications" for my website. I'm encountering a problem where I can't locate the correct synta ...

What is the syntax for populating an attribute on the same line as v-for in Vue.js?

I am currently working on a simple loop utilizing Vue to iterate over an array of objects and populate table rows. <tr v-for="user in users"> <td>{user.name}</td> <td>{user.id}</td> </tr> However, I also need to as ...

Adjust the visual presentation based on the chosen option at the top of a column JQchart

Can anyone offer assistance with creating three radio button fields that will display yearly, monthly, or weekly data on a column chart? I have searched through numerous examples in JQchart but haven't found one for this specific scenario. Are there o ...

Is there a way to simulate Pinia and vue-i18n simultaneously?

Exploring Vue 3's Composition API with a twist: The store.ts file import { ref, Ref } from 'vue'; import { defineStore } from 'pinia'; export const useStore = defineStore('store', () => { const isLoading: Ref<bo ...

Obtaining an identification using JQuery for content that is constantly changing

I am currently developing dynamic content tabs using PHP, with one of the objects being a datatable within the tab. In order to define the ID via PHP, I use the following code: PHP: echo '<table class="table table-striped table-bordered table-hov ...