PageAdapter.RenderBeginHyperlink 方法

定义

将一个超链接开始标记呈现到响应流。

重载

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)

将一个包含目标 URL 的超链接开始标记呈现到响应流。

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

将一个包含目标 URL 和一个访问键的超链接开始标记呈现到响应流。

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)

将一个包含目标 URL 的超链接开始标记呈现到响应流。

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)

参数

writer
HtmlTextWriter

包含可呈现特定于目标的输出的方法的 HtmlTextWriter

targetUrl
String

包含链接的目标 URL 的 String 值。

encodeUrl
Boolean

若使用 HtmlAttributeEncode(String) 对流输出进行编码,则为 true;否则为 false

softkeyLabel
String

要用作软键标签的 String 值。

示例

下面的代码示例演示如何从 类派生名为 的CustomPageAdapterPageAdapter类并重写 RenderBeginHyperlink 方法。 方法 RenderBeginHyperlink 将名为 src 的属性添加到超链接,其中包含对当前页的引用。 附加到的页面 CustomPageAdapter 中呈现的所有超链接都将具有 src 属性。

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

注解

方法 RenderBeginHyperlink 写入一个打开的超链接标记。 当 为 HtmlTextWriterwriter,此标记具有以下格式:

<a href=" targetUrl ">

继承者说明

PageAdapter 类继承时,可以重写 RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String) 方法,以便为打开的超链接标记编写不同的格式或写入其他标记属性。 例如, RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String) 基方法不为 softkeyLabel编写 特性。

另请参阅

适用于

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

将一个包含目标 URL 和一个访问键的超链接开始标记呈现到响应流。

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)

参数

writer
HtmlTextWriter

包含可呈现特定于目标的输出的方法的 HtmlTextWriter

targetUrl
String

包含链接的目标 URL 的 String 值。

encodeUrl
Boolean

若使用 HtmlAttributeEncode(String) 对流输出进行编码,则为 true;否则为 false

softkeyLabel
String

要用作软键标签的 String 值。

accessKey
String

将分配给待创建的链接的 accessKey 特性的 String 值。

例外

accessKey 的长度超过一个字符。

示例

下面的代码示例演示如何从 类派生名为 的CustomPageAdapterPageAdapter类并重写 RenderBeginHyperlink 方法。 RenderBeginHyperlink 将名为 的属性 src 添加到超链接,其中包含对当前页的引用。 附加到的页面 CustomPageAdapter 中呈现的所有超链接都将具有 src 属性。

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

注解

方法 RenderBeginHyperlink 写入一个打开的超链接标记。 当 是 对象HtmlTextWriterwriter,此标记具有以下格式:

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

继承者说明

PageAdapter 类继承时,可以重写 RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String, String) 方法,以便为打开的超链接标记编写不同的格式或写入其他标记属性。 例如, RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String, String) 基方法不为 softkeyLabel编写 特性。

另请参阅

适用于