0

I have defined the client-side click event for a link in the code-behind of an ASP.Net page. This code is in VB.Net as below and it uses Attributes collection of link control to add this client-side event. This event is executing as expected, but I cannot get a handle on the `event' argument for stopping event propgation.

Question

How can I access the event argument so that the stopPropagation method can be called to prevent the click event of a parent div from firing?

Dim link As System.Web.UI.WebControls.LinkButton = DirectCast(sender,
                                                      System.Web.UI.WebControls.LinkButton)

       link.Attributes.Add("onclick", "OnEmployeeClick('" & 
       dataRowItem("EmployeeId").ToString() & "','" & link.ClientID & "'); 
       return false; ")

UPDATE 1

As suggested by dandavis, I simply used the automatically available event argument, and it worked. I added new code of event.stopPropagation(); just before the OnEmployeeClick method call in the onclick definition in code-behind.

   Dim link As System.Web.UI.WebControls.LinkButton = DirectCast(sender,
                                                    System.Web.UI.WebControls.LinkButton)

   link.Attributes.Add("onclick", "event.stopPropagation();OnEmployeeClick('" & 
   dataRowItem("EmployeeId").ToString() & "','" & link.ClientID & "'); 
   return false; ")
4
  • 1
    as event usually (browsers insert the name in inline events for you), or as arguments[0] always Commented Jul 25, 2015 at 20:17
  • @dandavis, You mean the event argument is automatically available? if yes, is this a cross-browser thing? Commented Jul 25, 2015 at 20:21
  • Can you write this as an answer? Commented Jul 25, 2015 at 20:25
  • Ok. Thanks. I tried `'event' in my code and it works. Please post this. Commented Jul 25, 2015 at 20:27

1 Answer 1

1

Browsers automatically insert a formal parameter to inline events (those defined in HTML attributes), event.

that means that

<div onclick=alert(event.type)> Test </div>

should alert "click" when clicked, getting that event type string from the "magic" event parameter.

tested in FF, Ch, IE9.

see live demo to test your own browser for compat: http://pagedemos.com/s87ynjsgc7pe

Sign up to request clarification or add additional context in comments.

1 Comment

I used event.stopPropagation() as explained in UPDATE 1. It works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.