Solved

calling a javascript function with an asp hyperlink control?

Posted on 2004-09-22
4
20,379 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
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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