I am using Visual Studio 2012 and I want it to generate a simple onclick JavaScript event for an HTML button. I select the button in the Designer, go to properties, but there is no lightning bolt button for events. How to fix this?
I am using Visual Studio 2012 and I want it to generate a simple
Share
The event doesn’t exist because the HtmlButton Class is just a wrapper so the render can create a HTML output.
The base ASP.NET equivalent (with events) would be a System.Web.UI.WebControls.Button.
Now, all System.Web.UI.HtmlControls accept the AddAttribute method, which would allow you to programatically specify the control’s behavior. For example, let’s say you have this code on your HTML page:
And you want to add a javascript event. First, make it accessible by the server-side code:
Then, on the codebehind, add the content:
There you go. Hope it works for you!