Javascript 如何从asp.net HyperLink控件调用javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10537291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:45:01  来源:igfitidea点击:

how to call javascript from asp.net HyperLink control

javascriptasp.nethyperlink

提问by skhurams

I simply want to call a JavaScript function from asp.net hyperlink im using http://orangoo.com/labs/GreyBox/my requirement is to show thumbnail on hyperlink and on click show full image. Should I use another control? my code is below:

我只是想使用http://orangoo.com/labs/GreyBox/从 asp.net 超链接调用 JavaScript 函数,我的要求是在超链接上显示缩略图并单击显示完整图像。我应该使用另一个控件吗?我的代码如下:

<asp:HyperLink ID="Hyperlink" runat="server" CssClass="Screenshot" ImageUrl="App_Themes/White/Images/thmb_screenshot_1.png"
                        NavigateUrl="App_Themes/White/Images/screenshot_1.png" ToolTip="screenshot 1"   />   

<script language="javascript" type="text/javascript">     
    //Greybox Image popup window
    function OpenImage(url) {
        var caption = "Home";
        return GB_showImage(caption, url)
    }         
</script>

how can I use

我该如何使用

onclick="OpenImage(this.src);
or 
OnClientClick="OpenImage(this.src);

回答by KingOfHypocrites

I know this is old but to anyone interested, just add onclick and it will work fine. The extra attribute (even though it doesn't show up on intellisense) will pass through to the rendered markup.

我知道这很旧,但对于任何感兴趣的人,只需添加 onclick 即可正常工作。额外的属性(即使它没有出现在智能感知上)将传递到呈现的标记。

            <asp:HyperLink ID="HyperLink4" runat="server" onclick="logDownload();" NavigateUrl="~/download/Sample-Icons.zip">Download Icons</asp:HyperLink>

回答by James Johnson

If you use a LinkButtoninstead, you can use the OnClientClickproperty to execute a JavaScript function. Using the HyperLinkcontrol, you can use the NavigateUrlproperty like this:

如果使用 aLinkButton代替,则可以使用该OnClientClick属性来执行 JavaScript 函数。使用HyperLink控件,您可以NavigateUrl像这样使用属性:

<asp:HyperLink ID="Link1" runat="server"
    Text="Click Me!"
    NavigateUrl="javascript:OpenImage('App_Themes/White/Images/thmb_screenshot_1.png');">
</asp:HyperLink>

Here's an article that discusses it:
http://gchandra.wordpress.com/2007/09/27/call-javascript-function-inside/

这是一篇讨论它的文章:http:
//gchandra.wordpress.com/2007/09/27/call-javascript-function-inside/

回答by Sam Plus Plus

I know this is old, but for anyone looking to do this on the server side and not through the markup, you can do.

我知道这很旧,但是对于希望在服务器端而不是通过标记执行此操作的任何人,您都可以做到。

var hyperLink = new HyperLink { Text = "Display text", ID = "hyperlinkId"};
hyperLink.Attributes.Add("onclick", "javascriptMethod()");

回答by ilivewithian

It looks like you are basically there, what's wrong with this?

看起来你基本上就在那里,这有什么问题?

<asp:HyperLink ID="Hyperlink" runat="server" OnClientClick="OpenImage(this.src)" />