System.Web.UI.WebControls Namespace HyperLink Class
Initializes a new instance of the HyperLink class.
[ VB ]
Public Sub New ( )
[ C# ]
public HyperLink ( );
[ C++ ]
public: HyperLink ( );
[ JScript ]
public function HyperLink ( );
Use this constructor to programatically initialize a new instance of the HyperLink control at run time.
This method is useful when there is need to conditionally add or insert a new HyperLink control at some specified location on the page at run time, particularly when the number of HyperLink controls to generate are unknown at design time. Typical applications include programmatically adding HyperLink controls to dynamically generated list controls, such as the Repeater, DataList or DataGrid.
Below is some code snippet from our sample search results page that illustrates, among others, how to programatically add HyperLink controls to the ItemTemplate of a DataList control that is generated dynamically at run time. How many hyperlinks to render depends on how many records are returned in the results set.
void buildItemTemplate ( Control ctrl ) {
Table table; TableRow tr; TableCell td;
// setup search results
table = new Table ( );
// add a row for each item
tr = new TableRow ( );
// display search results item
td = new TableCell ( );
td.Controls.Add ( buildImageLink ( ) );
tr.Controls.Add ( td );
td = new TableCell ( );
td.Controls.Add ( buildItemLink ( ) );
tr.Controls.Add ( td );
...
table.Controls.Add ( tr );
IParserAccessor parser = ( ( IParserAccessor ) ( ctrl ) );
parser.AddParsedSubObject ( table );
}
private Control buildImageLink ( ) {
HyperLink ctrl = new HyperLink ( );
ctrl.ID = "asinImgLink";
...
return ctrl;
}
private Control buildItemLink ( ) {
HyperLink ctrl = new HyperLink ( );
ctrl.ID = "asinUrl";
return ctrl;
}
Show me
Akin to <asp:HyperLink> controls declared at design time, the Text and NavigateUrl properties of dynamically generated hyperlinks often are derived from a data source. In the case above, databinding is done via the ItemDataBound event handler of the DataList. Below shows a snippet of that code.
void setBinding ( Object src, DataListItemEventArgs e ) {
// handles the onItemDataBound event of the search results datalist
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
// set reference to current data record
DataRowView rowView = ( DataRowView ) e.Item.DataItem;
...
// get and set hyperlinks
HyperLink asinImgLink = ( HyperLink ) e.Item.FindControl ( "asinImgLink" );
asinImgLink.NavigateUrl = !isThirdParty ?
string.Format ( "product.aspx?asin={0}", rowView [ "Asin" ] ) :
string.Format ( "product.aspx?asin={0}&mode={1}", rowView [ "Asin" ], currentMode );
asinImgLink.ToolTip = product;
HyperLink asinUrl = ( HyperLink ) e.Item.FindControl ( "asinUrl" );
asinUrl.NavigateUrl = !isThirdParty ?
string.Format ( "product.aspx?asin={0}", rowView [ "Asin" ] ) :
string.Format ( "product.aspx?asin={0}&mode={1}", rowView [ "Asin" ], currentMode );
asinUrl.Text = product;
asinUrl.ToolTip = product;
asinUrl.Attributes [ "onMouseOver" ] = "status='" +
rowView [ "Catalog" ] + " : " + product + "';return true";
...
}
}
}
HyperLink Members