Muokkaa

Jaa


PageAdapter.RenderBeginHyperlink Method

Definition

Renders an opening hyperlink tag to the response stream.

Overloads

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)

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

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String, String)

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

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)

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

public:
 virtual void RenderBeginHyperlink(System::Web::UI::HtmlTextWriter ^ writer, System::String ^ targetUrl, bool encodeUrl, System::String ^ softkeyLabel);
public virtual void RenderBeginHyperlink (System.Web.UI.HtmlTextWriter writer, string targetUrl, bool encodeUrl, string softkeyLabel);
abstract member RenderBeginHyperlink : System.Web.UI.HtmlTextWriter * string * bool * string -> unit
override this.RenderBeginHyperlink : System.Web.UI.HtmlTextWriter * string * bool * string -> unit
Public Overridable Sub RenderBeginHyperlink (writer As HtmlTextWriter, targetUrl As String, encodeUrl As Boolean, softkeyLabel As String)

Parameters

writer
HtmlTextWriter

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

targetUrl
String

The String value holding the target URL of the link.

encodeUrl
Boolean

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

softkeyLabel
String

The String value to use as a soft key label.

Examples

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 );
        else
            url = targetUrl;
        writer.AddAttribute( "href", url );

        // Render the hyperlink opening tag with the added attributes.
        writer.RenderBeginTag( "a" );
    }
}
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Adapters

' A derived PageAdapter class.
Public Class CustomPageAdapter
    Inherits PageAdapter

    ' Override RenderBeginHyperlink to add an attribute that 
    ' references the referring page.
    Public Overrides Sub RenderBeginHyperlink( _
        ByVal writer As HtmlTextWriter, ByVal targetUrl As String, _
        ByVal encodeUrl As Boolean, ByVal softkeyLabel As String, _
        ByVal accessKey As String)

        Dim url As String

        ' Add the src attribute, if referring page URL is available.
        If Not (Page Is Nothing) Then
            If Not (Page.Request Is Nothing) Then
                If Not (Page.Request.Url Is Nothing) Then

                    url = Page.Request.Url.AbsoluteUri
                    If encodeUrl Then
                        url = HttpUtility.HtmlAttributeEncode(url)
                    End If
                    writer.AddAttribute("src", url)
                End If
            End If
        End If

        ' Render the accessKey attribute, if requested.
        If Not (accessKey Is Nothing) Then
            If accessKey.Length = 1 Then
                writer.AddAttribute("accessKey", accessKey)
            End If
        End If

        ' Add the href attribute, encode the URL if requested.
        If (encodeUrl) Then
            url = HttpUtility.HtmlAttributeEncode(targetUrl)
        Else
            url = targetUrl
        End If
        writer.AddAttribute("href", url)

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

    End Sub
End Class

Remarks

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(HtmlTextWriter, String, Boolean, String) method to write a different format for an opening hyperlink tag or to write additional tag attributes. For example, the RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String) base method does not write an attribute for softkeyLabel.

See also

Applies to

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String, String)

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

public:
 virtual void RenderBeginHyperlink(System::Web::UI::HtmlTextWriter ^ writer, System::String ^ targetUrl, bool encodeUrl, System::String ^ softkeyLabel, System::String ^ accessKey);
public virtual void RenderBeginHyperlink (System.Web.UI.HtmlTextWriter writer, string targetUrl, bool encodeUrl, string softkeyLabel, string accessKey);
abstract member RenderBeginHyperlink : System.Web.UI.HtmlTextWriter * string * bool * string * string -> unit
override this.RenderBeginHyperlink : System.Web.UI.HtmlTextWriter * string * bool * string * string -> unit
Public Overridable Sub RenderBeginHyperlink (writer As HtmlTextWriter, targetUrl As String, encodeUrl As Boolean, softkeyLabel As String, accessKey As String)

Parameters

writer
HtmlTextWriter

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

targetUrl
String

The String value holding the target URL of the link.

encodeUrl
Boolean

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

softkeyLabel
String

The String value to use as a soft key label.

accessKey
String

The String value to assign to the accessKey attribute of the link to create.

Exceptions

accessKey is longer than one character.

Examples

The following code example demonstrates how to derive a class named CustomPageAdapter from the PageAdapter class and override the RenderBeginHyperlink method. RenderBeginHyperlink 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 );
        else
            url = targetUrl;
        writer.AddAttribute( "href", url );

        // Render the hyperlink opening tag with the added attributes.
        writer.RenderBeginTag( "a" );
    }
}
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Adapters

' A derived PageAdapter class.
Public Class CustomPageAdapter
    Inherits PageAdapter

    ' Override RenderBeginHyperlink to add an attribute that 
    ' references the referring page.
    Public Overrides Sub RenderBeginHyperlink( _
        ByVal writer As HtmlTextWriter, ByVal targetUrl As String, _
        ByVal encodeUrl As Boolean, ByVal softkeyLabel As String, _
        ByVal accessKey As String)

        Dim url As String

        ' Add the src attribute, if referring page URL is available.
        If Not (Page Is Nothing) Then
            If Not (Page.Request Is Nothing) Then
                If Not (Page.Request.Url Is Nothing) Then

                    url = Page.Request.Url.AbsoluteUri
                    If encodeUrl Then
                        url = HttpUtility.HtmlAttributeEncode(url)
                    End If
                    writer.AddAttribute("src", url)
                End If
            End If
        End If

        ' Render the accessKey attribute, if requested.
        If Not (accessKey Is Nothing) Then
            If accessKey.Length = 1 Then
                writer.AddAttribute("accessKey", accessKey)
            End If
        End If

        ' Add the href attribute, encode the URL if requested.
        If (encodeUrl) Then
            url = HttpUtility.HtmlAttributeEncode(targetUrl)
        Else
            url = targetUrl
        End If
        writer.AddAttribute("href", url)

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

    End Sub
End Class

Remarks

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

<a href=" targetUrl " accessKey=" accessKey ">

Notes to Inheritors

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

See also

Applies to