Solved

How can I add onClick functionality to asp:MenuItem?

Posted on 2009-05-14
9
5,259 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 33
Simple Total function not working 8 30
Create a WebAPI using a generic return parameter 9 25
insert value of checklistbox checked 4 25
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

776 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