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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

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

Method 2

If you use a LinkButton instead, you can use the OnClientClick property to execute a JavaScript function. Using the HyperLink control, you can use the NavigateUrl property like this:

<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/

Method 3

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

Method 4

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x