LA.NET [EN]

Mar 22

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…

8 comments so far

  1. Kyle Baley
    7:05 pm - 3-23-2009

    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. luisabreu
    7:50 pm - 3-23-2009

    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. Kyle Baley
    3:21 pm - 3-24-2009

    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. luisabreu
    4:21 pm - 3-24-2009

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

  5. Kyle Baley
    5:32 pm - 3-25-2009

    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. luisabreu
    7:12 pm - 3-25-2009

    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. jitesh
    5:18 am - 11-18-2009

    nice topic……….

  8. test
    4:31 pm - 12-1-2009

    test