PageAdapter.RenderBeginHyperlink Method (HtmlTextWriter, String, Boolean, String)


Renders an opening hyperlink tag that includes the target URL to the response stream.

Namespace:   System.Web.UI.Adapters
Assembly:  System.Web (in System.Web.dll)

public virtual void RenderBeginHyperlink(
	HtmlTextWriter writer,
	string targetUrl,
	bool encodeUrl,
	string softkeyLabel


Type: System.Web.UI.HtmlTextWriter

The HtmlTextWriter containing methods to render the target-specific output.

Type: System.String

The String value holding the target URL of the link.

Type: System.Boolean

true to use HtmlAttributeEncode to encode the stream output; otherwise, false.

Type: System.String

The String value to use as a soft key label.

The RenderBeginHyperlink method writes an opening hyperlink tag. When writer is HtmlTextWriter, this tag has the following format:

<a href="targetUrl">

Notes to Inheritors:

When you inherit from the PageAdapter class, you can override the RenderBeginHyperlink method to write a different format for an opening hyperlink tag or to write additional tag attributes. For example, the RenderBeginHyperlink base method does not write an attribute for softkeyLabel.

The following code example demonstrates how to derive a class named CustomPageAdapter from the PageAdapter class and override the RenderBeginHyperlink method. The RenderBeginHyperlink method adds an attribute named src to a hyperlink, which contains a reference to the current page. All hyperlinks rendered in pages to which CustomPageAdapter is attached will have the src attribute.

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.Adapters;

// A derived PageAdapter class.
public class CustomPageAdapter : PageAdapter
    // Override RenderBeginHyperlink to add an attribute that 
    // references the referring page.
    public override void RenderBeginHyperlink(
        HtmlTextWriter writer, string targetUrl,
        bool encodeUrl, string softkeyLabel, 
        string accessKey )
        string url = null;

        // Add the src attribute, if referring page URL is available.
        if( Page != null && Page.Request != null &&
            Page.Request.Url != null )
            url = Page.Request.Url.AbsoluteUri;
            if( encodeUrl )
                url = HttpUtility.HtmlAttributeEncode( url );
            writer.AddAttribute( "src", url );

        // Add the accessKey attribute, if caller requested.
        if( accessKey != null && accessKey.Length == 1 )
            writer.AddAttribute( "accessKey", accessKey );

        // Add the href attribute, encode the URL if requested.
        if( encodeUrl )
            url = HttpUtility.HtmlAttributeEncode( targetUrl );
            url = targetUrl;
        writer.AddAttribute( "href", url );

        // Render the hyperlink opening tag with the added attributes.
        writer.RenderBeginTag( "a" );

.NET Framework
Available since 2.0
Return to top