4.6. HyperLink ControlA HyperLink control looks similar to a LinkButton control with a fundamental difference: the HyperLink control immediately navigates to the target URL without a postback, while the LinkButton control posts the form. If the LinkButton event handler chooses, it will navigate to the target URL. A HyperLink control behaves very similarly to an HTML control. The HyperLink control has four specific attributes:
Table 4-3. Special values of the Target attribute
The following example, HyperLinkDemo, demonstrates a HyperLink control. The content file is shown in Example 4-11. Since the HyperLink control does not post back to the server, there is no code-behind file in this example. Example 4-11. Default.aspx from HyperLinkDemo <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>HyperLink Control</title> </head> <body> <form id="form1" runat="server"> <div> <h1>HyperLink Control</h1> <asp:HyperLink ID="hypLink" runat="server" NavigateUrl="//localhost/websites/TargetPage.aspx" Target="_self" Font-Names="Impact" Font-Size="16" ToolTip="Click here to go to target page."> HyperLink to Target Page </asp:HyperLink> </div> </form> </body> </html> When the HyperLinkDemo page is run, it looks like Figure 4-5. For this page to work correctly as written, there must be a page called TargetPage.aspx located in the physical directory that corresponds to the websites virtual directory on your local machine. Figure 4-5. HyperLinkDemoThe HyperLink control is rendered on the client browser as an HTML anchor tag (that is, <a>) . You can verify this by examining the source code for the web page on your browser. |