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…

Advertisements

~ by Luis Abreu on March 22, 2009.

8 Responses to “The MVC framework: the Button helper”

  1. Do you know if someone has built a ButtonLink helper? I.e. one that acts like an ActionLink (i.e. when clicked, takes you to the specified action and controller) but looks like a button? I”ve built one but was thinking it should be possible out of the box.

  2. hum…well, I think that you probably shouldn”t do that with a button. That behaviour (navigating to other page through button click) should be done through an anchor or form submit, don”t you think?

  3. Yes, in theory. In practice, some customers like buttons. Besides, sometimes it makes sense. Like a Cancel button that takes you back to the home page for example.

  4. Well, JS in buttons don”t really work when JS is inactive, right?

  5. Nope, but that”s not a concern for internal sites at companies using a standard configuration.

    I”m not going to be able to defend this choice forever so I”ll cut to the short answer: the customer wants it. Given that, is it available in some form out of the box?

  6. Ok. I”m under the impression that that control does not exist (though you can easily add JS by passing a dictionary of HTML attributes to the current method helper.

  7. nice topic……….

  8. test

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: