?
Solved

How can I add onClick functionality to asp:MenuItem?

Posted on 2009-05-14
9
Medium Priority
?
5,440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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

752 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