[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 20748
  • Last Modified:

calling a javascript function with an asp hyperlink control?

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
bullrout
Asked:
bullrout
  • 2
3 Solutions
 
daffodilsCommented:
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
 
tusharashahCommented:
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
 
daffodilsCommented:
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
 
jnhorstCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now