What you can see here is a function called with parentheses, an argument provided to the function and the return false; statement that prevents the postback.
Here is a simple example of using the OnClientClicked event properly:
Note the value of the OnClientClicked property is only the function’s name and that AutoPostBack is false.
Following is the same functionality, implemented in the OnClientClicking event so that the postback can be cancelled conditionally:
The AutoPostBack property is not necessary because the event propagation is cancelled with the set_cancel(true) method. It has the same effect as having return false; at the end of your function.
At this point you are probably wondering what the two arguments that I am using are and where they come from. The answer is simple – following the MS AJAX convention these are the control instance that fired the event (so you can use its full Client-side API) and an event arguments object that has contextually meaningful properties and methods, depending on the control and the particular event. They are automatically provided to the event handler by our code.
Now that we’ve covered the basics, it is time for the more advanced stuff. We have to deliver more than expected, so our button has a rich client-side API. This allows you, for example, to easily add and remove handlers during runtime. There can even be multiple handlers for the same event to top it off. Copy this code in your VS and see how several functions can be executed:
As you can see, handlers can be added many times and removed just as many. The add_<event>(function) and remove_<event>(function) methods are what you need to make your pages more dynamic. They take a function as an argument and are available for the other events as well, e.g. add_clicking(clickingHandler).
The goodies do not end here – this approach is widely available in the entire suite, so it can be used for other RadControls as well and is not limited to RadButton.
Grab these simple snippets and paste them in your VS to click around and see RadButton’s power in action. You can extend them further with all the other customization options it offers – both in terms of functionality and appearance. Test it out, take a look at its online demos and I hope you will easily adopt this control in your projects.
Marin Bratanov is a Principal Technical Support Engineer in the Blazor division, after starting out in WebForms and going through Kendo UI. Ever since he joined Telerik in early 2011 as a novice, his main focus has been improving the services and customer care the company offers. Apart from work, Marin is an avid reader and usually enjoys the worlds of fantasy and Sci-Fi literature. You can find him on Twitter, Goodreads, LinkedIn and Facebook.
Subscribe to be the first to get our expert-written articles and tutorials for developers!