Web Forms - Working with ASP.NET server controls

Using Link Buttons

As a programmer, you have little control over what happens when the user clicks a hyperlink because the browser takes over. But what if you need to set a hyperlink's target on-the-fly when it's clicked, or if you want to ask the user to confirm that she wants to navigate to a new URL or execute some Visual Basic code when a link is clicked? For that, you use link buttons.

Link buttons look just like hyperlinks, but act like buttons, with both Click and Command events. When the corresponding hyperlink is clicked, you can take some action in code, not just let the browser automatically navigate to a new page. For example, the LinkButton example in the code for this book makes a message appear in a label with a dashed border when you click the link button, as you see in Figure 14.14.


Figure 14.14

The LinkButton example.

Like hyperlink controls, link buttons are supported with HTML <a> elements, but the code for link buttons is processed back at the server. Here's what the HTML for the link button you see in Figure 14.13 looks like:

<a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')"
style="Z-INDEX: 101; LEFT: 193px; POSITION: absolute; TOP: 169px"> Click Me!</a>

Link buttons are supported with the System.Web.UI.WebControls.LinkButton class; here's the hierarchy for this class:

System.Object System.Web.UI.Control
  System.Web.UI.WebControls.WebControl
    System.Web.UI.WebControls.LinkButton

You can find the significant public properties of System.Web.UI.WebControls.LinkButton objects in Table 14.7 and their significant public events in Table 14.8. (This class has no noninherited methods.) Note that as with other Web server controls, these tables do not list the significant properties, methods, and events this class inherits from the Control and WebControl classes—you can find them in Tables 12.1 to 12.5.

Table 14.7 Significant Public Properties of LinkButton Objects

Property Means
CommandArgument Returns or sets an (optional) argument holding text associated with the command specified with the CommandName property.
CommandName Returns or sets the command name for this link button.
Text Returns or sets the text displayed in the link button.

Table 14.8 Significant Public Events of LinkButton Objects

Event Means
Click Occurs when the link button was clicked.
Command Occurs when the link button was clicked and you've assigned text to the CommandName property.

The whole point of link buttons is that you can handle hyperlink events in code. In particular, you can add code to the Click and Command event handlers for link buttons, which you can't do for hyperlink controls. Like hyperlink controls, you set the text in a link button with the Text property. Unlike hyperlink controls, however, link buttons don't have a NavigateUrl propertyinstead, you use code in the Click and Command events to handle link button events.

You can see a link button at work in the LinkButton example in Figure 14.14. When the user clicks the hyperlink, a label with the text "Hello from VB .NET!" and a dashed border becomes visible, as you see in the figure. To make that label visible, all you need to do is to add code to the link button's Click event like this:

Private Sub LinkButton1_Click(ByVal _
  sender As System.Object, ByVal e As System.EventArgs) _
  Handles LinkButton1.Click
  Label1.Visible = True
End Sub

Here's another example; when the user clicks a link button in this case, the code displays a button with the caption "Navigate to Microsoft's Web Site?". If the user confirms her choice by clicking this button, the code navigates the browser to http://www.microsoft.com:

Private Sub Page_Load(ByVal sender As _
  System.Object, ByVal e As System.EventArgs) _
  Handles MyBase.Load
  Button1.Visible = False
End Sub

Private Sub LinkButton1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles LinkButton1.Click
  Button1.Text = "Navigate to Microsoft's Web Site?"
  Button1.Visible = True

End Sub

Private Sub Button1_Click(ByVal _
  sender As System.Object, ByVal e As System.EventArgs) _
  Handles Button1.Click
  Response.Redirect(" target="new">http://www.microsoft.com")
End Sub

Here's one more example, which sets the target of a link button on-the-fly, depending on the current time of day (which you can find with the Month, Hour, Minute, and Day functions if you pass them the current time, contained in the Visual Basic Now property). When the user clicks this link button, she will see a breakfast, lunch, or dinner menu, as appropriate (the Hour function returns the current hour of the day in 0–23 format):

Private Sub LinkButton1_Click(ByVal _
  sender As System.Object, ByVal e As System.EventArgs) _
  Handles LinkButton1.Click
  If Hour(Now) > 0 And Hour(Now) <= 11 Then
    Response.Redirect("BreakfastMenu.html")
  ElseIf Hour(Now) > 11 And Hour(Now) <= 17 Then
    Response.Redirect("LunchMenu.html")
  Else
    Response.Redirect("DinnerMenu.html")
  End If
End Sub

That's the whole idea behind link buttons—when the user clicks them, you can execute code, not just automatically jump to a new URL. And that's it for our look at Web server hyperlink and link button controls.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry