简体   繁体   English

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

[英]how to call javascript from asp.net HyperLink control

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. 我只想使用http://orangoo.com/labs/GreyBox/从asp.net超链接调用JavaScript函数我的要求是在超链接上显示缩略图,然后单击显示完整图像。 Should I use another control? 我应该使用另一个控件吗? my code is below: 我的代码如下:

<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);

I know this is old but to anyone interested, just add onclick and it will work fine. 我知道这是旧的,但对任何感兴趣的人,只需添加onclick,它将工作正常。 The extra attribute (even though it doesn't show up on intellisense) will pass through to the rendered markup. 额外属性(即使它没有显示在intellisense上)将传递给渲染的标记。

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

If you use a LinkButton instead, you can use the OnClientClick property to execute a JavaScript function. 如果使用LinkButton ,则可以使用OnClientClick属性来执行JavaScript函数。 Using the HyperLink control, you can use the NavigateUrl property like this: 使用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/

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()");

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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