The MVC framework: the Button helper
Today we’re going to keep looking at other helps available on the future assembly. The topic for today is: ButtonBuilder and using the available extension methods for adding HTML button type elements to the page. We’re going to start looking at the functionalities available on the ButtonBuilder class (keep in mind that you won’t probably be calling these methods from the pages; generally, you’ll end up using the extension methods we’ll be presenting at the end of the post).
Currently, the ButtonBuilder (static) class exposes 3 (static) methods:
- SubmitButton: used for inserting a <input type=”submit”> element to a form;
- SubmitImage: used for generating a <input type=”image”> element on a page;
- Button: used for adding <button> elements to a page.
Here’s a quick example of how you might use the Button method to add a <button> element and to configure it so that it shows an alert message when someone clicks on that button:
<%= Html.Button( "message",
"H",
HtmlButtonType.Button,
"printHello()") %>
As you can see, we’re using one of the HtmlHelper extension methods defined on the ButtonsAndLinkExtensions static class. Besides this method (and its overloads), the class exposes also several other methods that redirect to the methods presented on the previous list.
One of the things you should keep in mind is that, in this case, the name parameter is used only to set up the HTML name (or id) attributes on the generated HTML control. So, if you need to pass a value from the view data dictionary, you’ll have to do it explicitly.
And that’s it. There’s still more to say about the MVC framework, but I’ll leave it to another day…