Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlAnchor
Defines the destination of a hypertext link, or designates a named location within a document.
Declarative Syntax
<a id="accessID" runat="server"
href = "linkurl"
name = "bookmarkname"
onServerClick = "onServerClickHandler"
target = "linkedcontentframeorwindow"
title = "linkinformationaltip">
linktext
</a>
For information on the individual members of this class, see HtmlAnchor in the class library.
- 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 />
).
Like its HTML counterpart, the HtmlAnchor control is basically used to navigate from the client page to another page, or to another location within the same page.
In addition, HtmlAnchor enables programming of the HTML <a
> element.
You can use this control to dynamically modify the attributes of the <a
> element, display hyperlinks derived from a data source, and respond to events to manipulate properties of the control dynamically.
The following samples illustrate use of the HtmlAnchor control.
The following shows a simple way of 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.
The sample is implemented as follows.
- 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="/shared/images/earth.gif"
width="150" border=0
title="Save our Planet" visible=false />
</form>
- 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>
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 a given field in a data source.
And when databinding is used within templates in list controls such as the <asp:Repeater
>, <asp:DataGrid
>, <asp:DataList
>, and <asp:GridView
>, 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.
The above example applies several concepts and methods that are described elsewhere in this workshop. For particulars, see ADO.NET Primer, Introduction to Data Binding in Web Forms, Web Forms Server Controls Templates.
HtmlAnchor Class