[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

onMouseOver event not working in HyperLink

I cannot seem to get the following onMouseOver event to fire.
First the code:

      public class menuv2 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Table tblMenu;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  mUtils m = new mUtils();
                  DataSet ds = m.DisplayMenus("editor");
                  int rowct = ds.Tables["Menus"].Rows.Count;
                  int numcells = 1;

                  for (int j=0; j<rowct; j++)
                  {          
                        TableRow r = new TableRow();
                        DataRow dr = ds.Tables["Menus"].Rows[j];
                        for (int i=0; i<numcells; i++)
                        {
                              TableCell c = new TableCell();
                              HyperLink y = new HyperLink();
                              string offstring = "../graphics/"+dr["imageurl"].ToString();
                              string onstring = "somefile.png";
                              string lcstring = "<A onmouseover="+'"'+"MM_nbGroup("+"'"+"over"+"','"+"Restaurants"+"','"+"../graphics/restaurants-on.png"+"','',1"+")"+'"'+"</A>";
                              y.NavigateUrl = dr["redirect"].ToString();
                              y.Target = dr["target"].ToString();
                              y.ImageUrl = offstring;
                              y.ToolTip = dr["description"].ToString();
                              //LiteralControl lc = new LiteralControl(lcstring);
                              //y.Controls.Add(lc);
                              y.Attributes.Add("onMouseOver", "javascript:MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png','',1)");
                              c.Controls.Add(y);
                              r.Cells.Add(c);
                              
                        }
                        tblMenu.Rows.Add(r);
                  }
            }

This is an aspx page that drives a database driven menu system. The javascript call in the Attributes.Add is  Macromedia Dreamweaver code. It works when called directly in html. The source for this is embedded as javascript in the html of this aspx file. Thinking it might be the script, I tried ImageUrl.Replace, as well as y.src, with the same results. As you can see from the commented out code and "lcstring", I also tried building a LiteralControl....same result. Thinking it might be a contextual issue, I even tried building a Hyperlink outside of the Table tblMenu.... no luck. I read quire alot of similar frustrations with this particular Method. Any ideas?
0
jladolcetta
Asked:
jladolcetta
1 Solution
 
COBOLdinosaurCommented:
As the problem is in the client browser yu might want to post the generated HTML that the browser is looking at.  
As the majority of developers don't use ASP you just reduce the number of experts who might participate.

Cd&

0
 
davidlars99Commented:
try this  (hard way)

string lcstring = "<A href="+'"'+'#'+'"'+" onmouseover="+'"'+"MM_nbGroup("+"'"+"over"+"','"+"Restaurants"+"','"+"../graphics/restaurants-on.png"+"','',1"+")"+'"'+">My Link</A>";




or this (easy way)

string lcstring = "<A href='#' onmouseover=MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)>My Link</A>";
0
 
davidlars99Commented:
BTW, generated HTML was:

<A onmouseover="MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png','',1)"</A>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zuckeraCommented:
1. for a link to be a link you need a href.
2. your <A tag is not closed.
0
 
jladolcettaAuthor Commented:
How are you getting the double quote in front of "MM_nbGroup"? I tried inputting the lcstring the "easy way" and it didn't have quotes around the javascript function.
0
 
davidlars99Commented:
this how

string lcstring = "<A href='#' onmouseover="+'"'+"MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)"+'"'+">My Link</A>";
0
 
davidlars99Commented:
> I tried inputting the lcstring the "easy way" and it didn't have quotes

you don't need them it works without them..
0
 
davidlars99Commented:
or

string lcstring = "<A href='#' onmouseover="+"""+"MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)"+"""+">My Link</A>";
0
 
jladolcettaAuthor Commented:
sonething is wrong because the generated html comes out as             
<td><a title="Foods Dept." onMouseOver="<A href='#' onmouseover=&quot;MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)&quot;>My Link</A>" href="foods/index.aspx" target="_parent"><img title="Foods Dept." src="../graphics/editfoods-off.png" border="0" /></a></td>


I know &quot; is a " but I am pretty sure that I am not supposed to be seeing &quot (while else would a real " come out before the <A tag?).
The short version compiled but returned the above source code with IE reporting "errors on the page" which were "syntax errors" on each onmouseover line (there are four of them).
I tried the "long" version and got errors: the compiler complained about open quotes which would result with three &quot; in a row... i.e. you can't put a double quote in a quotation.

0
 
davidlars99Commented:
try this:

     public class menuv2 : System.Web.UI.Page
     {
          protected System.Web.UI.WebControls.Table tblMenu;
     
          private void Page_Load(object sender, System.EventArgs e)
          {
               mUtils m = new mUtils();
               DataSet ds = m.DisplayMenus("editor");
               int rowct = ds.Tables["Menus"].Rows.Count;
               int numcells = 1;

               for (int j=0; j<rowct; j++)
               {          
                    TableRow r = new TableRow();
                    DataRow dr = ds.Tables["Menus"].Rows[j];
                    for (int i=0; i<numcells; i++)
                    {
                         TableCell c = new TableCell();
                         HyperLink y = new HyperLink();
                         y.NavigateUrl = dr["redirect"].ToString();
                         y.Target = dr["target"].ToString();
                         y.ImageUrl = offstring;
                         y.ToolTip = dr["description"].ToString();
                         y.Attributes.Add("onMouseOver", "javascript:MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png','',1)");
                         c.Controls.Add(y);
                         r.Cells.Add(c);
                         
                    }
                    tblMenu.Rows.Add(r);
               }
          }
0
 
davidlars99Commented:
if you have to use  "lcstring" variable do it the easy way you don't have to have double quotes around every function...
lcstring = "<A href='#' onmouseover=MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)>My Link</A>";

and why would you want to add two same hyperlink in the same anchor tag?
y.Attributes.Add("onMouseOver", "javascript:MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png','',1)");

looks like dynamic version of hyperlink creation works better then manual!
0
 
jladolcettaAuthor Commented:
None of the above worked. I kind of think I have additional issues so I am posting the entire html and code behind. Maybe someone has some ideas about why this isn't working. I filled in the rest and made some additional changes. I am no longer getting javascript errors, however, I am also not getting the desired results on the mouseover and onclick attribs.

Well, here we go...Thanks to all for your help:

First the HTML:

<HTML>
      <HEAD>
            <title>menu</title>
            <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
            <meta content="C#" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            <script language="JavaScript" type="text/JavaScript">
            <!--
            function MM_preloadImages() { //v3.0
            var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
                  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
                  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
            }

            function MM_findObj(n, d) { //v4.01
            var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
                  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
            if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
            for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
            if(!x && d.getElementById) x=d.getElementById(n); return x;
            }

            function MM_nbGroup(event, grpName) { //v6.0
            var i,img,nbArr,args=MM_nbGroup.arguments;
            if (event == "init" && args.length > 2) {
                  if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
                  img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
                  if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
                  nbArr[nbArr.length] = img;
                  for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
                        if (!img.MM_up) img.MM_up = img.src;
                        img.src = img.MM_dn = args[i+1];
                        nbArr[nbArr.length] = img;
                  } }
            } else if (event == "over") {
                  document.MM_nbOver = nbArr = new Array();
                  for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
                  if (!img.MM_up) img.MM_up = img.src;
                  img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
                  nbArr[nbArr.length] = img;
                  }
            } else if (event == "out" ) {
                  for (i=0; i < document.MM_nbOver.length; i++) {
                  img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
            } else if (event == "down") {
                  nbArr = document[grpName];
                  if (nbArr)
                  for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
                  document[grpName] = nbArr = new Array();
                  for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
                  if (!img.MM_up) img.MM_up = img.src;
                  img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
                  nbArr[nbArr.length] = img;
            } }
            }
            //-->
            </script>
      </HEAD>
      <body id="bodytag" MS_POSITIONING="GridLayout" runat="server">
            <form id="Form1" method="post" runat="server">
                  <asp:table id="tblMenu" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"
                        BorderStyle="Outset" BorderColor="Lime" ForeColor="Lime" Runat="server"></asp:table></form>
      </body>
</HTML>

Now the code behind:
      public class menuv2 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Table tblMenu;
            protected HtmlGenericControl bodytag;
            private void Page_Load(object sender, System.EventArgs e)
            {
                  mUtils m = new mUtils();
                  DataSet ds = m.DisplayMenus("editor");
                  int rowct = ds.Tables["Menus"].Rows.Count;
                  int numcells = 1;
                  bodytag.Attributes.Add("language", "javascript");
                  bodytag.Attributes.Add("onload", "MM_preloadImages('restaurants-on.png')");
                  //Control bodycontrol = this.FindControl("bodytag");
                  for (int j=0; j<rowct; j++)
                  {          
                        TableRow r = new TableRow();
                        DataRow dr = ds.Tables["Menus"].Rows[j];
                        for (int i=0; i<numcells; i++)
                        {
                              TableCell c = new TableCell();
                              HyperLink y = new HyperLink();
                              string offstring = "../graphics/"+dr["imageurl"].ToString();
                              //string onstring = "http://www.villageeateries.com/graphics/restaurants-on.png";
                              string onstring = "restaurants-on.png";
                              //string lcstring = "<A onmouseover="+'"'+"MM_nbGroup("+"'"+"over"+"','"+"Restaurants"+"','"+"../graphics/restaurants-on.png"+"','',1"+")"+'"'+"</A>";
                              //string lcstring =  "<A href='#' onmouseover="+"\\\""+"MM_nbGroup("+"'"+"over"+"','"+"Restaurants"+"','"+"../graphics/restaurants-on.png"+"','',1"+")&quot;"+">My Link</A>";
                              //string lcstring = "<A href='#' onmouseover=MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)>My Link</A>";
                              //string lcstring = "<A href='#' onmouseover="+'"'+"MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)"+'"'+">My Link</A>";
                              //string lcstring = "<A onmouseover=MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)>My Link ";

                              //string lcstring = "<A href='#' onmouseover="+"""+"MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png',,1)"+"""+">My Link</A>";
                              y.NavigateUrl = dr["redirect"].ToString();
                              y.Target = dr["target"].ToString();
                              y.ImageUrl = offstring;
                              y.ToolTip = dr["description"].ToString();
                              //LiteralControl lc = new LiteralControl(lcstring);
                              //y.Controls.Add(lc);
                              //y.Attributes.Add("onMouseOver", "javascript:MM_nbGroup('over','Restaurants','../graphics/restaurants-on.png','',1)");
                              //y.Attributes.Add("onMouseOver", lcstring);
                              //y.Attributes.Add("onMouseOver", "MM_nbGroup('over','group1','Restaurants','"+onstring+"'"+",1)");
                              //y.Attributes.Add("onClick","MM_nbGroup('down','group1','Restaurants','"+onstring+"'"+",1)" );
                              y.Attributes.Add("language", "javascript");
                              y.Attributes["onMouseOver"] = "MM_nbGroup('over','group1','Restaurants','"+onstring+"'"+",1)";
                              y.Attributes["onClick"] = "MM_nbGroup('down','group1','Restaurants','"+onstring+"'"+",1)" ;
                              //y.Attributes["onload"] = "MM_preloadImages('restaurants-on.png')";
                              y.Attributes["onMouseOut"] = "MM_nbGroup('out')";
                              c.Controls.Add(y);
                              r.Cells.Add(c);
                              
                        }
                        tblMenu.Rows.Add(r);
                  }
            }

Notes:
Commented out code was previous tries. I added the bodytag after discovering that that is where I would "preload images". Adding the "language" as an "Attribute" on each Image was a result of some reading I was doing that lead me to believe that there may be anomalies in Attributes that I am unaware of. I discovered the index feature of the Attributes property a few days ago. It seems to generate good html. Any other questions, feel free to ask.
0
 
davidlars99Commented:
I tryied to compile the code, but it gives an error message about "mUtils m = new mUtils();" meanning that, cannot instantiate "mUtils()". do you have something else going on there? because I've never seen anything like that before...

0
 
jladolcettaAuthor Commented:
I'm changing things up a bit because the javascript is not working, and, frankly, is not very modular or elegant. Instead I am going to use a flash button. The following is how the html is supposed to look:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>menu</title>
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" Content="C#">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema">     content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body bgColor="#000000">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="575" height="200" id="frames" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="acctgButton.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="frames.swf" quality="high" bgcolor="#ffffff" width="575" height="200" name="frames" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</body>
</HTML>

I can't add a flash button to the comments here but I am betting that you might have one or two add your disposal .. :-))

Can you give me the code behind for this in C#? It will NOT work inside an <A> tag, by the way.
0
 
davidlars99Commented:
>>  I'm changing things up a bit because the javascript is not working, and, frankly, is not very modular or elegant.

looks to that you don't know what you're doing...
0
 
jladolcettaAuthor Commented:
Gee, Dave, nothing personal... I am sorry you took offense to my comments.....The question of modularity and elegance is a result of the fact that this particular non-working code affects many pages, and would be difficult to migrate. I realized that flash buttons have built in mouseover and mouseclick events. Now I have the task of trying to understand how to write code behind that automates calling the buttons I write. I hope this better explains what I am trying to accomplish.

This is a very small piece of a very large project. Again: anyone who honestly wants to help is welcome to. And by the way, if I knew what I was doing (all the time) forums like this would be no use to me. Your solutions so far have not worked and neither have mine. If you want to help I would greatly appreciate it. Otherwise, please allow someone else to help me.

0
 
davidlars99Commented:
I thought I gave you the solution, according to your problem you had hyperlink which was producing syntax error in C# and I corrected it and I really don't know what else is your problem in that project, look into javascript function maybe that's where the problem comes...
0
 
davidlars99Commented:
no problem, thanks for your attention to this topic. I consider it just right   :)
0
 
jladolcettaAuthor Commented:
Just a final comment, meant as instructive. Not all of us out there are gurus on one subject (i.e.javascript, C#, Systems Admin, Network Engineering....etc....) and some of us don't want to be. What has always given me an edge is my ability to see "the forest for the trees", so to speak, and pick the right tool for the job. This is another type of knowledge that a guru doesn't necessarily have. Sometimes we need a guru to point us to the right tool. I think David did a great job of doing that, even if it wasn't his favorite tool.

I was never expecting "the level of support" that the moderator implied. It would have been sufficient to have said that "whenever a big change in direction" takes place, it should be issued as a new question. I would have understood. Please realize that we, your clients, don't necessarily state our agenda when we ask a question, nor do we necessarily have to abide by your agendas. Our projects are important to us, however, and they guide us in our decision making. As a matter of fact, your client's projects are their own agenda(s), something I think you must keep in mind when answering technical questions.

One last bid of advice. Don't get mired in "technical religions".  The tool is the means to an end... not the end in and of itself.

Thanks for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now