Solved

How can I add onClick functionality to asp:MenuItem?

Posted on 2009-05-14
9
5,286 Views
Last Modified: 2012-05-07
Hello.  I have a little bit of JavaScript for Google Analytics that I want to add to an ASP MenuItem.

If the link weren't an ASP MenuItem, it would be like this:

<a href="http://taxes.co.frederick.va.us/comm_rev/door_hanger/DoorHangerApp/type.aspx" onClick="javascript: pageTracker._trackPageview('/Buttons/DoorHanger');">Door Hanger</a>

But it is an ASP MenuItem, so I have the code attached (other menu items removed for simplification).  Since OnClick is not part of MenuItem, what can I do to get the same functionality?

Jeremy
<asp:Menu ID="Menu1" runat="server">
  <Items>
     <asp:MenuItem NavigateUrl="http://taxes.co.frederick.va.us/comm_rev/door_hanger/DoorHangerApp/type.aspx" Text="Reassessment Notice" Value="home_taxes" />
  </Items>
</asp:Menu>

Open in new window

0
Comment
Question by:mrcoulson
9 Comments
 
LVL 7

Expert Comment

by:zwei
ID: 24385595
Add runat="server" to it and in the code you do a postback on selectedIndexChanged with a response.redirect.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24385728
There is no direct way I could find.
However following a trick will do the job. Remove NavigateUrl and Text property from MenuItem in the ASPX file and use the code below in Page_Load or Page_Init or any other event you wish. Make sure to replace JavaScript alert sample code with yours.

Menu1.Items.Item(0).Text = "<a href=""http://taxes.co.frederick.va.us/comm_rev/door_hanger/DoorHangerApp/type.aspx"" onClick=""javascript: alert('hello');"">" & "Actual Text to Show-Can be any variable" & "</a>"

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 24385796
Wait.  Response.Redirect to the JavaScript?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 7

Expert Comment

by:zwei
ID: 24385828
Oh you're not using any cs files, only asp?
0
 

Author Comment

by:mrcoulson
ID: 24385891
Menu1.Items.Item(1).Text = "<a href=""http://taxes.co.frederick.va.us/comm_rev/door_hanger/DoorHangerApp/type.aspx"" onClick=""javascript: pageTracker._trackPageview('/Buttons/Real_Estate_Reassessment');"">" & "Actual Text to Show-Can be any variable" & "</a>";

moghazali: I'm missing a semi-colon.  Where?  Hmmm...

zwei: Right.  I used to not know how to use the codebehind.
0
 
LVL 4

Accepted Solution

by:
baiju_nagori earned 500 total points
ID: 24386803
Try this
<asp:Menu ID="Menu1" runat="server">
  <Items>
     <asp:MenuItem NavigateUrl="javascript: pageTracker._trackPageview('/Buttons/DoorHanger'); window.location='http://taxes.co.frederick.va.us/comm_rev/door_hanger/DoorHangerApp/type.aspx';" Text="Reassessment Notice" Value="home_taxes" />
  </Items>
</asp:Menu>

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 24387101
baiju_nagori: If it's that simple...  Stand by.
0
 

Author Comment

by:mrcoulson
ID: 24387119
Man!  I think that's it.  But I won't know for sure until tomorrow when my clicks start to show up in Google Analytics.  So, we'll have to wait until then...

Jeremy
0
 

Author Comment

by:mrcoulson
ID: 24394717
baiju_nagori:That works!  I see the clicks getting tracked in Google Analytics.  Thanks!
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

Suggested Solutions

Title # Comments Views Activity
100% tall div not scrollable on iPhone 3 20
linq, c# 8 24
asp.net open new page without popup blocker 8 19
Running powershell scripts from ASP.NET 6 31
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

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