JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Link Buttons

Link buttons let you handle hyperlinks with Visual Basic code. As with hyperlink controls, you set the text in a link button with the Text property, and as with hyperlink controls, link buttons are supported with HTML <a> elements. However, you can add code to the Click and Command event handlers for link buttons, which you can't do for hyperlink controls.

You can see a link button at work in the LinkButtons example on the CD-ROM in Figure 17.7. In this case, when the user clicks the hyperlink, I make a label with the text "Sorry, no more information is available" and a solid border visible, as you can see in that figure. To make that label visible, all I need to do is to add this code to the link button's Click event:

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

And that's all it takes. Here's WebForm1.aspx for the LinkButtons example:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="LinkButtons.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>LinkButtons example</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content=
    "http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">
<asp:LinkButton id=LinkButton1 style="Z-INDEX: 101; LEFT: 162px;
POSITION: absolute; TOP: 75px" runat="server">here</asp:LinkButton>
<asp:Label id=Label3 style="Z-INDEX: 104; LEFT: 191px; POSITION:
absolute; TOP: 75px" runat="server">for more information.</asp:Label>
<asp:Label id=Label2 style="Z-INDEX: 103; LEFT: 128px; POSITION:
absolute; TOP: 75px" runat="server">Click</asp:Label>
<asp:Label id=Label1 style="Z-INDEX: 102; LEFT: 101px; POSITION:
absolute; TOP: 130px" runat="server" Width="243px" Height="26px"
BorderStyle="Solid" BorderWidth="1px" Visible="False">Sorry, no more
information is available.</asp:Label>

    </form>

  </body>
</HTML>

And here's WebForm1.aspx.vb for the LinkButtons example:

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents LinkButton1 As _
        System.Web.UI.WebControls.LinkButton

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal _
        e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal _
        e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

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




JavaScript Editor Free JavaScript Editor     JavaScript Editor