More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

Examples to use client side method or function for a hyperlink Options · View
hong
Posted: Sunday, July 18, 2010 11:11:43 AM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
There are several ways to use client-side functions or methods for a hyperlink.
In an html anchor. For example, the following code specifies the method to track page view using Google Analytics via onclick:
Code:
<a onclick="pageTracker._trackPageview ('/http_bettereducation_com_au_Results_vce_aspx');"
href="http://bettereducation.com.au/Results/vce.aspx" target="_blank">VCE School Ranking</a>

For ASP.NET controls like Hyperlink, MenuItem or LinkButton:
1. Use their NavigateUrl property and set it to "javascript: <MyMethod>"
2. Client method with string literals as arguments
Code:
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("WebSite","http://{0}") %>'
   Target="_blank" Text='<%# Eval("Name") %>' onClick="javascript: pageTracker._trackPageview
('/coachingSchool/<SpecificSchoolName>');"></asp:HyperLink>


3. Client method with dynamic strings (determined at run-time) as arguments
Use the Attributes property of the controls to add your OnClick attribute:
Code:
hyperLink.Attributes.Add("OnClick","<MyMethod>")


Code:
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("WebSite","http://{0}") %>'
                        Target="_blank" Text='<%# Eval("Name") %>' onload="HyperLink2_Load"></asp:HyperLink>

Code:
    protected void HyperLink2_Load(object sender, EventArgs e)
    {
        HyperLink link = (HyperLink)sender;
        string script = string.Format(@"javascript: pageTracker._trackPageview('/coachingSchool/name/{0}');", link.Text.Replace(' ', '_'));
        link.Attributes.Add("onClick", script);
    }


4. Add a LinkButton control and set the OnClientClick property to the client side method.
Sponsor
Posted: Sunday, July 18, 2010 11:11:43 AM
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.285 seconds.