Udostępnij za pośrednictwem


HtmlAnchor Server Control Declarative Syntax

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

<a 
    EnableViewState="False|True" 
    Href="string" 
    Id="string"
    Title="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnServerClick="OnServerClick event handler"
    OnUnload="OnUnload event handler"
    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"></a>
<a id="anchor1" runat="server"></a>

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 Evalmethod 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(ByVal sender As Object, ByVal e As EventArgs)
    anchor1.HRef = "https://www.microsoft.com"
End Sub
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 ItemTemplatethat displays the specified field in the data source as a hyperlink.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlAnchor Control</title>
</head>

<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>

   <br />
   <form id="Form1" runat="server">
      <asp:Repeater id="MyRepeater" runat="server">
         <ItemTemplate>
            Link for
            <a id="A1" href='<%# DataBinder.Eval(Container, _
                         "DataItem.StringValue", _
                         "detailspage.aspx?id={0}") %>' 
               runat="server">
               <%# DataBinder.Eval(Container, "DataItem.StringValue") %>
            </a>
         </ItemTemplate>
      </asp:Repeater>
   </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlAnchor Control</title>
</head>
<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>
   <br />
   <form id="Form1" runat="server">
      <asp:Repeater id="MyRepeater" runat="server">
         <ItemTemplate>
            Link for
            <a id="A1" href='<%# DataBinder.Eval(Container,
                         "DataItem.StringValue",
                         "detailspage.aspx?id={0}") %>' 
               runat="server">
               <%# DataBinder.Eval(Container, "DataItem.StringValue") %>
            </a>
            <br />
         </ItemTemplate>
      </asp:Repeater>
   </form>
</body>
</html>

See Also

Reference

HtmlAnchor

Other Resources

HTML Server Controls