System.Web.UI.WebControls Namespace
Renders a link to another Web page.
Visibility |
Constructor |
Parameters |
public |
HyperLink |
( )
|
|
Use the HyperLink control to create a link to another Web page.
The control is typically displayed as text specified by the Text property, or as an image specified by the ImageUrl property. The destination page is specified using the NavigateUrl property.
NOTE: If both the Text and ImageUrl properties are set, the ImageUrl property takes precedence. If the image is unavailable, the text in the Text property is displayed. In browsers that support ToolTip functionality, the Text property also serves as the ToolTip.
HyperLink is particularly useful for programmatically generating links at a specified location on the page at run time, in which the text and destination page are determined dynamically, either from user input or from some other process in your code.
In certain cases, as when hyperlinks are used within templated controls, the Text, ImageUrl, and NavigateUrl properties, or portions thereof, are usually obtained dynamically from a data source.
Below is some code snippet from our sample storefront, which dynamically generates a variable set of hyperlinks that lets users navigate to the different pages available for a given catalog. The total number of page links to dislay depends on how many items are in the results set at run time. In cases where there is more than one set of links to render, the current set is initially set to the first, and thereafter is based on user input.
void buildPager ( ) {
// initialize pager variables
int startpage, endpage, pageno;
startpage = ( currentLinksSet * 10 ) - 9;
endpage = totalPages >= startpage + 9 ? startpage + 9 : totalPages;
// generate page links
for ( pageno = startpage; pageno <= endpage ; pageno ++ ) {
if ( pageno == currentPage ) {
Label thisPage = new Label ( );
thisPage.Text = pageno.ToString ( );
thisPage.ForeColor = System.Drawing.Color.Snow;
pager.Controls.AddAt ( pager.Controls.Count, thisPage );
}
else {
HyperLink pageLink = new HyperLink ( );
pageLink.Text = pageno.ToString ( );
pageLink.ForeColor = System.Drawing.Color.Khaki;
pageLink.NavigateUrl = string.Format
( "catalogs.aspx?mode={0}&node={1}&sub={2}&page={3}&sort={4}",
mode, node, activenode, pageno, sortorder );
...
pager.Controls.AddAt ( pager.Controls.Count, pageLink );
}
LiteralControl spacer = new LiteralControl ( " " );
pager.Controls.AddAt ( pager.Controls.Count, spacer );
}
if ( currentLinksSet < totalLinksSets ) {
// do button for next set of pager links
HyperLink pageLink = new HyperLink ( );
pageLink.Text = "»";;
pageLink.ForeColor = System.Drawing.Color.Khaki;
pageno = endpage + 1;
pageLink.NavigateUrl = string.Format
( "catalogs.aspx?mode={0}&node={1}&sub={2}&page={3}&sort={4}",
mode, node, activenode, pageno, sortorder );
...
pager.Controls.AddAt ( pager.Controls.Count, pageLink );
}
if ( startpage > 10 ) {
// do button for previous set of pager links
HyperLink pageLink = new HyperLink ( );
pageLink.Text = "«";;
pageLink.ForeColor = System.Drawing.Color.Khaki;
pageno = startpage - 1;
pageLink.NavigateUrl = string.Format
( "catalogs.aspx?mode={0}&node={1}&sub={2}&page={3}&sort={4}",
mode, node, activenode, pageno, sortorder );
...
pager.Controls.AddAt ( 0, pageLink );
LiteralControl spacer = new LiteralControl ( " " );
pager.Controls.AddAt ( 1, spacer );
}
pagerTracker.Text = "Viewing Page " + currentPage + " of " +
string.Format ( "{0:n0}", totalPages );
pagerPanel.Visible = true;
}
Show me
For other examples illustrating use of this control, see the individual member types of this class. For syntax information, see HyperLink in ASP.NET Syntax for Web Controls.
HyperLinkColumn LinkButton Introduction to the HyperLink