Solved

calling a javascript function with an asp hyperlink control?

Posted on 2004-09-22
4
20,448 Views
Last Modified: 2011-09-20
Hi there,

I wanted to know the syntax for calling a js function inside an asp hyperlink control?

Sean - thanks in advance


<asp:HyperLink runat="server" Text="<%# DataBinder.Eval(Container.DataItem, &quot;shopname&quot;)%>" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "shop")%>' ID="Hyperlink1" NAME="Hyperlink1"/></td>
0
Comment
Question by:bullrout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:daffodils
daffodils earned 166 total points
ID: 12122371
HyperLink control does not expose any events.

It might be better to call use a LinkButton and then add the javascript function call as an attribute.

In the Page_Load
 
 myLinkButton.Attributes.Add("onclick", "someJavascriptFuncion();");

0
 
LVL 18

Accepted Solution

by:
tusharashah earned 168 total points
ID: 12122702
The method shown above is perfect, but you can even call JavaScript using following method:

------------------------------------------------------------------------------------------------

function openexample()
{
    var props = 'location=no,menubar=no,toolbar=no,height=500,width=500';
    window.open('example.pdf,'Example',props,true);
}

<td><asp:hyperlink id="lnkpdf" runat="server" target="_blank" navigateurl="javascript:openexample();">example</asp:hyperlink></td>

------------------------------------------------------------------------------------------------

-tushar
0
 
LVL 8

Expert Comment

by:daffodils
ID: 12123288
Hey bullrout,

Haven't you asked this question before?? what's with that solution ??
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21074951.html
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 166 total points
ID: 12127215
bullrout:

With a hyperlink control you can set the NavigateUrl property to call the js function like this:

hl.NavigateUrl = "javascript:fnName();"

Assuming here the fnName is the name of the function.  You could, of course, pass parameters to the js function as needed.

daffodils is right also that the LinkButton control can be used.  The LinkButton control renders to the browser as an <a> tag.  The href="..." attribute of the <a> calls the __doPostBack() JavaScript routine that is written to the browser by ASP.NET to fire the postback.  So if you add an onclick event as daffodils shows, you can call another js routine *before* the postback happens.  I like to use this to disable the link button client-side before the postback to guarantee no further clicks on the button.

John
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question