Ryan Ternier

Killer ASP.NET ninja coding monkeys do exist!

Get rid of regular buttons, use link buttons

For the past 3 years I have been pushing my managers and especially the teams I work with that the web applications we make need to be visually appealing. The graphics designers I know can do amazing things. My friend Steve Sereda is an amazing graphics designer and he created some awesome skins for me for the time we worked together.

I've done a few posts leading up to this one. It all started with my post on UpdatePanel, FireFox and the Default Button, where I talked about how to get the default button working in FireFox. At this point I was in the process of moving all buttons over to fancy LinkButtons. I then wrote about CSS Pseudo Classes and their uses (which are many), and finally I finished my last post on A LinkButton, DefaultButton, UpdatePanel and FireFox, where I discuss how to get FireFox to use a LinkButton (Anchor tag) as a default button.

So let's talk about Buttons. Buttons provide functionality, and 99% of users know when they click on a button that some sort of action is going to happen. Many buttons look like this: 

button

Now, besides the ugly border my snipping tool puts in, that button isn't that great. But should that matter? Isn't a button there to tell the user that clicking it will invoke some sort of procedure? Correct. By general standards Buttons should invoke functions, while links should be directional. However, who said a button had to be grey and ugly?

ASP.NET has the LinkButton. It gets rendered as an <a /> (anchor) tag, and because of that we can do something like this:

3 buttons

Wow, that looks way better than regular buttons. What about this:

delete

I've grown tired of using JavaScript confirm boxes. I've decided to switch to modal windows. (yes yes I know my spacing isn't right....). You can see the nice presentation, and notice the spacing... isn't that amazing?!

There's a trick to these buttons, and unfortunately you do have to put more work into them. I could make a button like this a userControl/CompositeControl, but for what I need them for, that's too much work.

Let's look at the markup I use for these buttons. This will give you more clarity when you see the CSS.

<div class="button">

   <asp:LinkButton ID="btnSave" runat="Server" CssClass="bSave" Text="Save User" OnClick="btnSave_Click"  ValidationGroup="userDetails"  UseSubmitBehavior="false" />

</div>

<div class="button">

    <asp:LinkButton ID="btnCancel" runat="Server" CssClass="bCancel" Text="Cancel" OnClick="btnCancel_Click" CausesValidation="False"   UseSubmitBehavior="false" />

</div>

<div class="button">

    <asp:LinkButton ID="btnDeleteUser" runat="Server" CssClass="bDelete" Text="Delete" UseSubmitBehavior="false" />

</div>
<div class="clear"></div>

You see that I have 3 div's, and in each div I have a LinkButton. Under the LinkButtons I have a single div with the "clear" class. My clear class does clear:both;. It clears the floats that all of these div's have.

Here's the CSS

.button

{

    background-color:#84A1D6;

    border:solid 2px #415D9B;

    margin:5px;

    min-width:70px;

    height:20px;

    float:left;

    padding-left:3px;

}

 

 

.button a

{

    display:block;

    padding:2px 5px 2px 20px;

    min-width:70px;

}

 

.button:hover

{

    background-color: #a0bdf2;

}

 

.button a:active

{

    border-top:solid 1px black;

}

 

 

/* Button Style */

.button a.bLogin

{

    background-image: url(Images/Login.png);

    background-position:left;

    background-repeat:no-repeat;   

}

 

.button a.bSave

{

    background-image: url(Images/Save.png);

    background-position:left;

    background-repeat:no-repeat;   

}

 

.button a.bCancel

{

    background-image: url(Images/Cancel.png);

    background-position:left;

    background-repeat:no-repeat;   

}

You can notice that each .button class is floating left. I need to use Block elements for these buttons so I can apply padding, margin's etc. You cannot do that to an inline element. Seeing they're block, I want them line up, so I float them.  You can also see that my .button a contains the same width as the parent .button, this is because I want the entire button to be clickable, not just the link portion.

I am also using pseudo classes for this. You can see the :hover and the :active. This gives my buttons "hover" and "click" styles without having to code any javascript. (IE7 and FF).

And that is all you need to do.

Like I said, it's a tiny bit more markup and some small CSS, however, the outcome of this work is well worth it. It gives a whole new presentation to your application.

Comments

Will Green said:

Link buttons are BAD! A link is meant to be a non-destructive action; that is, they can only trigger HTTP GET requests. Buttons are meant to trigger possibly destructive actions; typically, this is through HTTP POST requests.

When you mix the two, you are in for trouble. Why? Google indexes your site by following links. If the link performs a destructive action, BOOM! Adios data!

If you really need the functionality of a button but the styleability of an anchor, use an HtmlButton element; that is a <button> tag with the runat="server" attribute. With the HtmlButton element, you can include ANY HTML markup you would like.

"Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content."

See www.w3.org/.../forms.html  

# October 25, 2007 9:26 PM

Ryan Ternier said:

@Will Green,

Thanks for the advice. I'll have to take a closer look at the <button> element.

I can see how Google would cry from that. I guess a next blog post of mine will be how to do that to the <button>.

# October 25, 2007 10:51 PM

Mark Smith said:

Why can't you just style the input that is rendered for a standard button or have I missed something? For example, try this (you'll have to create your own background image if you want to see that appear):

.defaultButton {border: outset 2px #415D9B;background-color:#84A1D6;background-image:url('buttonbg.png');color:#fff;width:70px;background-repeat:no-repeat;}

<input type="submit" value="OK" class="defaultButton" />

# October 26, 2007 3:57 AM

Jannik Anker said:

I'll second what Will said, and add the usability perspective. For users, links are exactly what Will also describes; a non-obtrusive action, whereas buttons are supposed to perform some kind of "final" action (like "send the email", "sign me up" and so on).

Another thing is that users know the appearance of a button when they see it, and knows what to expect from it, so to speak. Personally, having to use a fancy blue "button" with a non-standard icon to delete something makes me slightly sad :-s

The technique, however, is nicely described!

# October 26, 2007 6:08 AM

Ryan Ternier said:

The nice thing about this is that users do not under know that those are links, as they look like buttons.  The final output of this I believe is a nice alternative to those that don't like the regular button look, and want to deliver a more user friendly application.

The last project I was lead on it was required that each page have 3 save buttons:

-Save

-Save and Add New

-Save and Close.

You can believe the stress I had on how to make it look nice, buttons like these allowed me to put different icons beside the save, so users would remember which ones were which.

I guess I'll have to perfect my technique :)  

# October 26, 2007 10:07 AM

Ryan Ternier said:

@Mark Smith

I have tried that, however it renderes differently in some browsers, which is what I can't have when building a pricey application.

As far as changing everything else, your style works great, but it won't have the full control that I was looking for. 

The full version of this would allow me to hover of my containing div, and the :hover would increase the height, showing 1-5 more "buttons" under it. That would effectively be like a "Combo Button". 

# October 26, 2007 10:08 AM

AndrewSeven said:

I like linkbutton because it can be a link or a button.

I can make the page work and the html/css person can make the decision about which it will be ;)

# October 26, 2007 3:57 PM

Josh Stodola said:

Yeah dude, LinksButtons are no good.  Especially since your only defense is that you can make it look better.  That's not true at all.  Ever head of <input type="image" />?

You can style a regular button quite a bit, too, just not as much.  Link Buttons are ok for certain things, but I avoid them when possible.  I'd rather have a hidden button (with style='display:none;') and then have an anchor tag link invoke the click of the button (<a href='javascript:void(0);' onclick='document.getElementById('HiddenButton1').click();'>) than a LinkButton!

Best regards...

# October 26, 2007 4:35 PM

Ryan Ternier said:

<input type="image" /> is what an <asp:imageButton /> gets rendered as. The main reason i didn't use that one was because of some of the styling issues.

For internal applications this process has no flaws, except by the purists. However, after looking into having a <button id="mybutton" runat="server>..</button> approach, it seems mine isn't perfect yet :)

# October 26, 2007 5:32 PM

KAmran Shahid said:

I have feel of the exact problem as you described.

I will surely try your idea next time.

But one thing i want's to ask is how to change the onmouse over and onmouse out image for link button.

previously for the imagebutton i have use the attribute for that

# October 27, 2007 7:53 AM

Ryan Ternier said:

the image buttons has a src. I don't believe a :hover pseudo class (CSS) will work for that. For that you might need to use JavaScript.. but I'm not sure (i'll have to play with it).

# October 27, 2007 2:17 PM

javed said:

http://www.umundo.com/channel

open the video in video player in ie & fire fox play button is not working in fire fox

# January 15, 2008 3:24 AM

asp net button link said:

Pingback from  asp net button link

# May 2, 2008 7:07 PM

Web Developer Bangladesh said:

thanks !

# June 24, 2009 4:21 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)