語言

PageAdapter.RenderBeginHyperlink 方法

定義

會將開啟的超連結標籤導向回應串流。

多載

名稱 Description
RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)

會呈現包含回應串流目標網址的開場超連結標籤。

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

會渲染一個開啟的超連結標籤,包含目標網址及回應串流的存取鍵。

RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)

會呈現包含回應串流目標網址的開場超連結標籤。

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

持有連結目標網址的 String 值。

encodeUrl
Boolean

true用HtmlAttributeEncode(String)來編碼串流輸出;否則,。 false

softkeyLabel
String

String作為軟鍵標籤的價值。

範例

以下程式碼範例示範如何從該CustomPageAdapter類別衍生出一個命名PageAdapter為類別的類別,並覆蓋該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 方法會寫入開場超連結標籤。 當 writer 時 HtmlTextWriter,此標籤格式如下:

<a href=" targetUrl ">

給繼承者的注意事項

當你從 PageAdapter 該類別繼承時,可以覆寫 RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String) 為開場超連結標籤寫不同格式的方法,或是寫出額外的標籤屬性。 例如,RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String)基礎方法不會寫入屬性。softkeyLabel

另請參閱

適用於

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

會渲染一個開啟的超連結標籤,包含目標網址及回應串流的存取鍵。

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

持有連結目標網址的 String 值。

encodeUrl
Boolean

true用HtmlAttributeEncode(String)來編碼串流輸出;否則,。 false

softkeyLabel
String

String作為軟鍵標籤的價值。

accessKey
String

String要指派給accessKey要建立連結屬性的值。

例外狀況

accessKey 長度超過一個字元。

範例

以下程式碼範例示範如何從該CustomPageAdapter類別衍生出一個命名PageAdapter為類別的類別,並覆蓋該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 方法會寫入開場超連結標籤。 當 writer 是 HtmlTextWriter 物件時,此標籤格式如下:

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

給繼承者的注意事項

當你從 PageAdapter 該類別繼承時,可以覆寫 RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String, String) 為開場超連結標籤寫不同格式的方法,或是寫出額外的標籤屬性。 例如,RenderBeginHyperlink(HtmlTextWriter, String, Boolean, String, String)基礎方法不會寫入屬性。softkeyLabel

另請參閱

適用於