简体   繁体   中英

Adding mouseover attribute in c# code behind

I'm using ASP.NET and in the code behind I need to add an attribute which makes text2 appear when the user puts the mouse over text1; below is what I currently have.

JS:

function show(name) {
        document.getElementById(name).visible = true;
    }

C#:

text2.Attributes.Add("onmouseover", "show(" + text2.ClientID +")");

HTML:

<asp:HyperLink runat="server" ID="text2" Visible="false"/>
<asp:HyperLink runat="server" ID="text1" rel="external" />

Two things here:

  1. Visible="false" is not a styling, it is a server-side property. Setting it false means that the control won't be rendered at all. So it won't even exist on the client side, and of course there is no way to access it via javascript. Instead consider assigning a CSS class to it or, at the very least, inline style like style="visibility: hidden"

  2. You want to wrap the id of the control into quotes. Say if your control ends up with id "blah_blah_text2", your javascript will look like

     show(blah_blah_text2); 

    so js will try to evaluate "blah_blah_text2" as a variable, and will fail of course. Instead you want

     show('blah_blah_text2'); 

    so

     "show('" + text2.ClientID +"')" 

Also I am not quite sure what visible = true; stands for. There is no such property in DOM objects as far as I know. Did you actually mean document.getElementById(name).style.visibility = "visible" ?

which makes text2 appear when the user puts the mouse over text1; below is what I currently have.

Html title attribute serves this purpose.

Try this:

text2.Attributes.Add("title", text2.ClientID);

希望您正在寻找:

text2.Attributes.Add("onmouseover", "show('" + text2.ClientID +"')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM