Using the HyperLink Control


Using the HyperLink Control

The HyperLink control enables you to create a link to a page. Unlike the LinkButton control, the HyperLink control does not submit a form to a server.

For example, the page in Listing 2.32 displays a hyperlink that randomly links to a page in your application.

Listing 2.32. ShowHyperLink.aspx

[View full width]

<%@ Page Language="VB" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR /xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     Sub Page_Load()         lnkRandom.NavigateUrl = GetRandomFile()     End Sub     Function GetRandomFile() As String         Dim files As String() = Directory.GetFiles(MapPath(Request.ApplicationPath), "*.aspx")         Dim rnd As New Random()         Dim rndFile As String = files(rnd.Next(files.Length))         Return Path.GetFileName(rndFile)     End Function </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show HyperLink</title> </head> <body>     <form  runat="server">     <div>     <asp:HyperLink                  Text="Random Link"         Runat="server" />     </div>     </form> </body> </html> 

In the Page_Load event handler in Listing 2.32, a random file name from the current application is assigned to the NavigateUrl property of the HyperLink control.

The HyperLink control supports the following properties (this is not a complete list):

  • Enabled Enables you to disable the hyperlink.

  • ImageUrl Enables you to specify an image for the hyperlink.

  • NavigateUrl Enables you to specify the URL represented by the hyperlink.

  • Target Enables you to open a new window.

  • Text Enables you to label the hyperlink.

Notice that you can specify an image for the HyperLink control by setting the ImageUrl property. If you set both the Text and ImageUrl properties, then the ImageUrl property takes precedence.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net