0

Is there a way to add javascript functions to an asp.net button other than the way that is similar to: (C#)

Button link;
link.attributes.add("onmouseover", "functioncall()");
1
  • you write it on the client side. use libraries like jQuery Commented Feb 19, 2014 at 4:06

1 Answer 1

1

This is the most straight-forward way, quick and dirty...

<asp:Button ID="btn" runat="server" onmouseover="functioncall();"></asp:Button>

You should also consider using unobtrusive javascript with the aid of jQuery, by adding custom attributes...

<asp:Button ID="btn" runat="server" data-whatever="value"></asp:Button>

Then hook up the function on page load using jQuery...

$(function(){
    $('[data-whatever=value]').click(functioncall);
};
Sign up to request clarification or add additional context in comments.

2 Comments

From trying your first solution I get a warning that says that onmouseover is not a valid attribute of the element 'Button', should I just leave it alone and move on?
Yup, just ignore it, the button will indeed be parsed to an HTML button (or submit input) at runtime and it will be correctly render with the onmouseover attribute

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.