Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I add onClick functionality to asp:MenuItem?

Posted on 2009-05-14
9
Medium Priority
?
5,545 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

886 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