aspxtreme

HtmlAnchor Control Syntax

ASP.NET Syntax   ASP.NET Syntax for HTML Controls


Defines the destination of a hypertext link, or designates a named location within a document.

Declarative Syntax

For information on the individual members of this class, see HtmlAnchor in the class library.

Syntax Notes

  • Target values must begin with a letter, except when specifying the following special values that begin with an underscore: _blank, _self, _parent, and _top.
  • This control requires an opening and closing tag ( or an opening tag with a trailing slash /> ).

Working with HtmlAnchor

The HtmlAnchor control is used to navigate from the client page to another page, or to another location within the same page.

HtmlAnchor enables programming of the HTML <a> element. You can use this control to dynamically modify the attributes and properties of the <a> element, display hyperlinks from a data source, and control events to generate HtmlAnchor controls dynamically.

The following samples illustrate use of the HtmlAnchor control.


Generating the HtmlAnchor Text Attribute Dynamically

The following example shows how authors can dynamically generate the textual content of an HtmlAnchor control based on a given condition. In this case, the link text changes depending on whether the associated <img> control is visible or not.

Dynamic HtmlAnchor Sample
Run Sample   View Source

The sample is implemented as follows.

  1. Within the <body> of a Web Forms document, declare a <form> control that contains the HtmlAnchor and the associated image control.
    <form runat="server">
       <p><a id="showhideLink" runat="server" 
          onServerClick = "ShowHideImage" />
    
       <p><img id="image" runat="server" 
          src = "/aspxtreme/shared/images/earth.gif" 
          width = "100" height = "100" border=0 
          alt = "Save our Planet" visible=false />
    
    </form>
  2. Within the <head> of the page, define the event handlers that will assign the text to be displayed in the HtmlAnchor, using the control's InnerText property.
    <script language="C#" runat="server">
    void Page_Load ( Object sender, EventArgs e ) {
       // initialize link text
       if ( !IsPostBack ) {
          showhideLink.InnerText = image.Visible ? "Hide Image" : "Show Image";
       }
    }
    
    void ShowHideImage ( Object Src, EventArgs E ) {
       image.Visible = image.Visible ? false : true;
       showhideLink.InnerText = image.Visible ? "Hide Image" : "Show Image";
    }
    </script>

Binding Data to the HtmlAnchor Control

The below example illustrates using the simple yet powerful databinding feature of ASP.NET.

Databinding enables authors to dynamically generate hyperlinks on a Web Forms page using values from given fields in a data source. And when databinding is used within templates in list controls such as the <asp:Repeater>, <asp:DataGrid>, and <asp:DataList>, only a single declaration is needed to generate each hyperlink item in the list. Note that the method used in this sample not only generates the link text, but also the querystring parameter of the url, which determines what argument is passed along to each link.

DataBinding to the HtmlAnchor Control
Run Sample   View Source

The above example applies several concepts and methods that are described elsewhere in this workshop. For particulars, see ASP.NET Data Access Basics, Introduction to Data Binding in Web Forms, Web Forms Server Controls Templates.

See Also

HtmlAnchor Class



Books and more ...


Suggested Reading

Need a break ?



Previous page Back to top Next page

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support aspxtreme