The reason this tutorial is called 'Buttons and LinkButtons' is because there
is no real reason to have separate tutorials on them, since the properties,
methods and general syntax is the same. They have the same general
functionality, and the only difference is that the button, itself, looks like a
regular button on the page, but a LinkButton looks like a text link. It is not,
however, in any way, related to a hyperlink, which, at first glance, is what it
resembles.
If you think you knew how to work with buttons before - think again.
The basic use of the button, entails an event handler (or subroutine) to do
some action needed, when the button is clicked. So, the button control's html
would look like this:
<asp:Button id="btn1" Text="Click Me" onclick="btn1_Click" runat="server" />
Then, from this, youc an see that, to perform some action, it looks to the 'btn1_Click'
event handler (the subroutine that handles, or takes care of processing the
action:
Sub btn1_Click(sender As Object, e As EventArgs)
' Do whatever you need here
End Sub
This still works just like it did, with earlier versions of ASP.Net. However,
with the advent of 2.0, buttons now have much more power built in. Before, you
could incorporate multiple buttons with similar functionality, using only 1
subroutine for all of them, but it was very limited. Let's say there are two
buttons, which share the same subroutine, called 'btn1' and 'btn2'. The way you
could handle this (you still can do it this way) would be something like this:
Sub Button1_Click(sender As Object, e As EventArgs)
if sender.ID='btn1' then
' do processing here
elseif sender.ID='btn2' then
' do some other processing
end if
End Sub
Here's where it starts getting better, with ASP.Net v2.0. To begin with,
before, since there was a standard set of arguments (as there still are), in the
basic required signature of the subroutine for a button to postback, there was
no way to pass another argument to the routine. Well, now, you can, using
'Commands. The signature looks a little different for the subroutine, in this
case:
Sub Report_Arguments (sender As Object, Args As CommandEventArgs)
' do your processing here
End Sub
Here, you can use new properties in the button control ('OnCommand', 'CommandName',
and 'CommandArgument'). 'OnCommand' is used in place of the 'OnClick'
property. Then, we can use any combination of 'CommandName' and 'CommandArgument'
in the shared subroutine (or event handler), to process the event. With the
combination of the ID property along with these new properties, the Button and
LinkButton have become quite powerful. You can even use the 'CommandArgument'
property to send specific data, to use in a 'Where' clause, for an DataSource's
SelectCommand. Just think about it. The possibilites are quite phenomenal.
And, if that's not enough, now, it's even possible to use Javascript, for
MouseOver events, to change the appearance of the buttons.
<asp:Button id=Button1 runat="server"
Text="Click Me"
Width="125px"
onmouseover="this.style.backgroundColor='#D0D0D0';this.style.fontWeight='bold'"
onmouseout="this.style.backgroundColor='buttonface';this.style.fontWeight='normal'" />
With all this, then, being able to set a default button for a specific
container/form, as you can now see, from this tutorial, ASP.Net v2.0 has been
feeding the old Button control, apparently with a steady diet of steriods! Those
things that we wished were there before are now there, ready for use.