• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5760
  • Last Modified:

How can I add onClick functionality to asp:MenuItem?

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
mrcoulson
Asked:
mrcoulson
1 Solution
 
zweiSoftware DeveloperCommented:
Add runat="server" to it and in the code you do a postback on selectedIndexChanged with a response.redirect.
0
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
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
 
mrcoulsonAuthor Commented:
Wait.  Response.Redirect to the JavaScript?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
zweiSoftware DeveloperCommented:
Oh you're not using any cs files, only asp?
0
 
mrcoulsonAuthor Commented:
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
 
baiju_nagoriCommented:
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
 
mrcoulsonAuthor Commented:
baiju_nagori: If it's that simple...  Stand by.
0
 
mrcoulsonAuthor Commented:
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
 
mrcoulsonAuthor Commented:
baiju_nagori:That works!  I see the clicks getting tracked in Google Analytics.  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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