<asp:LinkButton> Control
The <asp:LinkButton> control displays a clickable
text link that calls a subprogram. It is the text equivalent of the
<asp:Button> control and should not be confused with an
<asp:HyperLink> control that issues a URL request. The general format for a
LinkButton control is shown below.
Figure 5-9. General format for <asp:LinkButton >control.
An id is not required unless the LinkButton itself is referenced
in a script. The Text attribute provides the text label for the link.
The OnClick event handler identifies a subprogram to run when the
link is clicked. OnCommand,
CommandName, and CommandArgument properties call a
subprogram that is shared by other links to pass along information about processing to perform.
PostBackUrl posts to a different page from the current page.
Calling a Subprogram
A single LinkButton calls a subprogram by coding an OnClick event
handler. The following button operates in this way. The called subprogram simply assigns a text
string to the accompanying Label control. Notice that the signature for a subprogram called by
a LinkButton contains the same EventArgs argument as a subprogram
called by a Button control.
Figure 5-10. Using a LinkButton to call a subprogram.
<SCRIPT Runat="Server">
Sub Link_Click (Src As Object, Args As EventArgs)
Message.Text = "The Link_Click subprogram was run."
End Sub
</SCRIPT>
<form Runat="Server">
<asp:LinkButton Text="Call Link_Click" OnClick="Link_Click" Runat="Server"/>
<asp:Label id="Message" Runat="Server"/>
</form>
Listing 5-14. Coding a LinkButton control.
Scripting Command Links
The LinkButton also can be configured as a command link so that a single subprogram
that services multiple LinkButtons can determine which link was clicked and take appropriate
action for the chosen link. As in the case of a command button, a command link has a
CommandName property to pass processing control information. If
additional information is needed for script processing, the LinkButton can pass a
CommandArgument to the subprogram.
In the following example, two LinkButtons call a single subprogram, passing
CommandName and CommandArgument arguments. The subprogram
reports on values that are passed. You should recognize this application as identical to the
previous example using command Buttons. The only difference is in the use of LinkButtons
to call the subprogram.
Figure 5-11. Using command links to call a subprogram.
<SCRIPT Runat="Server">
Sub Report_Arguments (Src As Object, Args As CommandEventArgs)
PassedValues.Text = "<b>Src.id</b> = " & Src.id & "<br/>"
PassedValues.Text &= "<b>Args.CommandName</b>= " & _
Args.CommandName & "<br/>"
PassedValues.Text &= "<b>Args.CommandArgument</b>= " & _
Args.CommandArgument & "<br/>"
End Sub
</SCRIPT>
<form Runat="Server">
<asp:LinkButton id="FirstButton" Runat="Server"
Text="Report Arguments"
OnCommand="Report_Arguments"
CommandName="Do This"
CommandArgument="With This Value"/>
<asp:LinkButton id="SecondButton" Runat="Server"
Text="Report Arguments"
OnCommand="Report_Arguments"
CommandName="Do That"
CommandArgument="With That Value"/>
<p><asp:Label id="PassedValues" Runat="Server"/></p>
</form>
Listing 5-15. Code to script a pair of command links.
Using LinkButtons for Database Display
The following example is a minor rewrite of the previous application of command Buttons to
display different sets of book types from the BooksDB.mdb database.
These LinkButtons are configured as command buttons to call a subprogram to issue different SQL
statements to create display tables through a GridView control.
View Book Types
Database
Graphics
Hardware
Software
Systems
Web
| BookID | BookTitle | BookPrice |
| DB111 | Oracle Database | $69.99 |
| DB222 | Databases in Depth | $29.95 |
| DB333 | Database Processing | $136.65 |
| DB444 | Access Database Design | $34.95 |
| DB555 | SQL Server 2005 | $29.99 |
Figure 5-12. Using command LinkButtons to issue SQL statements for book displays.
Script and control coding for this output is shown below. All LinkButtons have
CommandName values indicating the BookType field from the database
to be used for selecting subsets of records. These BookType values,
passed as Args.CommandName through the subprogram argument list, are
concatenated within an SQL statement to issue an appropriate command to retrieve records with
the matching value.
<SCRIPT Runat="Server">
Sub Show_Books (Src As Object, Args As CommandEventArgs)
Dim SQLString As String
SQLString = "SELECT BookID, BookTitle, BookPrice FROM Books " & _
"WHERE BookType = '" & Args.CommandName & "'"
BookSource.SelectCommand = SQLString
End Sub
</SCRIPT>
<form Runat="Server">
<h3>View Book Types</h3>
<asp:LinkButton Runat="Server"
Text="Database"
OnCommand="Show_Books"
CommandName="Database"
Width="80"/>
<asp:LinkButton Runat="Server"
Text="Graphics"
OnCommand="Show_Books"
CommandName="Graphics"
Width="80"/>
<asp:LinkButton Runat="Server"
Text="Hardware"
OnCommand="Show_Books"
CommandName="Hardware"
Width="80"/><br/>
<asp:LinkButton Runat="Server"
Text="Software"
OnCommand="Show_Books"
CommandName="Software"
Width="80"/>
<asp:LinkButton Runat="Server"
Text="Systems"
OnCommand="Show_Books"
CommandName="Systems"
Width="80"/>
<asp:LinkButton Runat="Server"
Text="Web"
OnCommand="Show_Books"
CommandName="Web"
Width="80"/>
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT BookID, BookTitle, BookPrice FROM Books
WHERE BookType = 'Database'"/>
<p><asp:GridView DataSourceID="BookSource" Runat="Server"/></p>
</form>
Listing 5-16. Code to issue SQL command depending on CommandName of clicked LinkButton.
Both Button and LinkButton controls provide the same functionality. Their choice is a matter
of visual preference about whether you wish to display buttons or text links to activate scripts.