Partager via


How to: Add HyperLink Web Server Controls to a Web Forms Page

You can add a hyperlink to your Web Forms page by placing a HyperLink Web server control on the page and associating it with a URL. You can specify that HyperLink controls render as either text or as graphics.

  1. Add an <asp:HyperLink> element to the page. For syntax, see HyperLink Web Server Control Declarative Syntax.

  2. Specify the format of the link in the page by doing one of the following:

    • To create a text link, set the control's Text property. You can include HTML formatting in the property. For example, you can format an individual word in the text as bold by placing a <B> tag around it in the Text property.

    • To create a graphical link, set the control's ImageUrl property to the URL of a .gif, .jpg, or other Web graphic file.

      NoteNote

      If you set both the ImageUrl and Text properties, the ImageUrl property takes precedence.

  3. Set the NavigateUrl property to the URL of the page to which you want to link.

    Security noteSecurity Note

    The URL that is associated with a hyperlink points to an external resource. If you point to a resource that you do not own, be sure it is safe for your users to work with. For more information, see How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings.

  4. Optionally, set the ID of a target window or frame in which to display the linked page. You can either specify a window by name or use predefined target values, such as _top, _parent, and so on.

    NoteNote

    You can change the appearance of the link text — for example, whether it is underlined — by using styles. For details, see ASP.NET Web Server Controls and CSS Styles.

    The following example shows how you can set the properties of a HyperLink control at run time. The method handles a Button control's Click event and sets the control's link text and target page.

    Protected Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
        Hyperlink1.Text = "Home"
        Hyperlink1.NavigateUrl = "https://www.contoso.com"
    End Sub
    
    protected void Button1_Click (object sender, System.EventArgs e)
    {
        this.HyperLink1.Text = "Home";
        this.HyperLink1.NavigateUrl = "https://www.contoso.com";
    }
    

See Also

Reference

HyperLink Web Server Control Overview