다음을 통해 공유


HtmlAnchor Control

Creates a server-side control that maps to the <a> HTML element and allows you link to another Web page.

<a id="programmaticID"
   href="linkurl"
   name="bookmarkname"
   OnServerClick="onserverclickhandler"
   target="linkedcontentframeorwindow"
   title="titledisplayedbybrowser" 
   runat="server" >
linktext
</a>

Remarks

Use the HtmlAnchor control to programmatically control an <a> HTML element. The <a> HTML element allows you to create a hyperlink that allows you to move to another location on the page or to another Web page. The HtmlAnchor control must be well formed with an opening and closing tag. You can specify the caption for the control by placing text between the opening and closing tags. This server control is commonly used 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.

You can specify the location to display the new Web page by using the Target property. Target values must begin with a letter in the range from a to z (case insensitive), except the following special values that begin with an underscore: _blank, _self, _parent, and _top.

You can dynamically generate the URL to which you want the HtmlAnchor control to link. To generate the HRef property dynamically, declare an HtmlAnchor control in an HTML document. For example:

<a id="anchor1" runat="server">

Note   Remember to embed the HtmlAnchor control inside the opening and closing tags of an HtmlForm control.

Next, write an event handler that assigns a URL to the HRef property of the HtmlControl.

Although the HtmlAnchor control does not directly support binding to a data source, it is possible to generate hyperlinks from the values of a field in a data source. First bind the data source to a list control, such as the Repeater. Next declare an HtmlAnchor control inside the list control. Finally add inline code for the value of the HRef property that uses the Eval method of the DataBinder class to specify the field to use.

Example

The following example demonstrates how to dynamically associate a URL with an HtmlAnchor control when the Page_Load event occurs.

Sub Page_Load(sender As Object, e As EventArgs)
   anchor1.HRef = "https://www.microsoft.com"
End Sub
[C#]
void Page_Load(object sender, EventArgs e) 
{ 
   anchor1.HRef = "https://www.microsoft.com";
}

The following example demonstrates how to include an HtmlAnchor control within a Repeater control. Data is bound to the Repeater control, while an HtmlAnchor control is placed in the ItemTemplate that displays the specified field in the data source as a hyperlink.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script runat="server">
   Sub Page_Load(sender As Object, e As EventArgs)
      Dim dt As New DataTable()
      Dim dr As DataRow
      dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
      dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
      dt.Columns.Add(New DataColumn("DateTimeValue", GetType(DateTime)))
      dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))
      dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
      Dim i As Integer
      For i = 0 To 8
         dr = dt.NewRow()
         dr(0) = i
         dr(1) = "Item " + i.ToString()
         dr(2) = DateTime.Now
         If i Mod 2 <> 0 Then
            dr(3) = True
         Else
            dr(3) = False
         End If
         dr(4) = 1.23 *(i + 1)
         dt.Rows.Add(dr)
      Next i
      MyRepeater.DataSource = New DataView(dt)
      MyRepeater.DataBind()
   End Sub
</script>

<body>
   <h3>Data Binding with the HtmlAnchor</h3>
   <p>
   <form runat=server>
      <asp:Repeater id="MyRepeater" runat="server">
         <ItemTemplate>
            Link for
            <a href='<%# DataBinder.Eval(Container, _
                         "DataItem.StringValue", _
                         "detailspage.aspx?id={0}") %>' 
               runat="server">
               <%# DataBinder.Eval(Container, "DataItem.StringValue") %>
            </a>
            <p>
         </ItemTemplate>
      </asp:Repeater>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script runat="server">
   void Page_Load(Object sender, EventArgs e) 
   {
      DataTable dt = new DataTable();
      DataRow dr;
      dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
      dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
      dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
      dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
      dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
      for (int i = 0; i < 9; i++) 
      {
         dr = dt.NewRow();
         dr[0] = i;
         dr[1] = "Item " + i.ToString();
         dr[2] = DateTime.Now;
         dr[3] = (i % 2 != 0) ? true : false;
         dr[4] = 1.23 * (i+1);
         dt.Rows.Add(dr);
      }
      MyRepeater.DataSource=new DataView(dt);
      MyRepeater.DataBind();
   }
</script>

<body>
   <h3>Data Binding with the HtmlAnchor</h3>
   <p>
   <form runat=server>
      <asp:Repeater id="MyRepeater" runat="server">
         <ItemTemplate>
            Link for
            <a href='<%# DataBinder.Eval(Container,
                         "DataItem.StringValue",
                         "detailspage.aspx?id={0}") %>' 
               runat="server">
               <%# DataBinder.Eval(Container, "DataItem.StringValue") %>
            </a>
            <p>
         </ItemTemplate>
      </asp:Repeater>
   </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlAnchor Class