Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert two OnClick event handlers into an .ascx user control?

Bearing in mind I have started a new job which demands I use Umbraco and ASP.NET - platforms/languages I've only been toying with for 2 days and am a total noob at - I have a query regarding inserting a Google Analytics event tracking code into a line of an .ascx file.

Here goes: I've drilled down into the specific line I'm required to insert a .gaq tracking code into; it's an onclick handler that corresponds to a submit button on a contact form. The line is:

<asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" CausesValidation="true" CssClass="btnSubmit floatRight">
<span>Submit</span>
</asp:LinkButton>

I'd like to add an event tracker to that OnClick property in the form of "_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])" and set up a relevant Event menu in GA. Only thing is, I don't know how to add the code into the existing OnClick setup - it's not a case of separating the two with a semi-colon, as I could do with JavaScript entries in normal HTML (eg, onclick="blah(); dah();").

Anyone able to tell me how to set two event handlers in one OnClick property in ASP.NET? If I sound murky on this it's because this language is extremely new to me.

Cheers.

like image 603
sliemm Avatar asked Jun 13 '13 07:06

sliemm


Video Answer


1 Answers

Use OnClientClick which is used to run JavaScript Before the page posts back. Something like:

<asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" OnClientClick="_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])" CausesValidation="true" CssClass="btnSubmit floatRight">
<span>Submit</span>
</asp:LinkButton>

Another solution would be to do:

Page.ClientScript.RegisterStartupScript(this.GetType(), ="_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])", true);

in the codebehind eventhandler for the click. This will output the script so that it is run when the page is loaded by the browser.

Hope this helps!

like image 185
mortb Avatar answered Oct 20 '22 16:10

mortb