ClientScriptManager.RegisterClientScriptBlock 方法

定義

Page 物件註冊用戶端指令碼。

多載

RegisterClientScriptBlock(Type, String, String)

使用型別、索引鍵和指令碼常值,向 Page 物件註冊用戶端指令碼。

RegisterClientScriptBlock(Type, String, String, Boolean)

使用型別、索引鍵、指令碼常值,以及表示是否加入指令碼標記的布林值,向 Page 物件註冊用戶端指令碼。

RegisterClientScriptBlock(Type, String, String)

使用型別、索引鍵和指令碼常值,向 Page 物件註冊用戶端指令碼。

public:
 void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script);
public void RegisterClientScriptBlock (Type type, string key, string script);
member this.RegisterClientScriptBlock : Type * string * string -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String)

參數

type
Type

要註冊的用戶端指令碼型別。

key
String

要註冊的用戶端指令碼索引鍵。

script
String

要註冊的用戶端指令碼常值。

範例

下列程式碼範例示範 如何使用 RegisterClientScriptBlock 方法。

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client script on the page.
    String csName = "ButtonClickScript";
    Type csType = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(csType, csName))
    {
      StringBuilder csText = new StringBuilder();
      csText.Append("<script type=\"text/javascript\"> function DoClick() {");
      csText.Append("Form1.Message.value='Text from client script.'; }");
      csText.Append("</script>");
      cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
    }
  }
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>
<%@ Page Language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
        ' Define the name and type of the client script on the page. 
        Dim csName As [String] = "ButtonClickScript"
        Dim csType As Type = Me.[GetType]()
        
        ' Get a ClientScriptManager reference from the Page class. 
        Dim cs As ClientScriptManager = Page.ClientScript
        
        ' Check to see if the client script is already registered. 
        If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
            Dim csText As New StringBuilder()
            csText.Append("<script type=""text/javascript""> function DoClick() {")
            csText.Append("Form1.Message.value='Text from client script.'; }")
            csText.Append("</script>")
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
        End If
    End Sub
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

備註

用戶端腳本是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。 只有一個具有指定類型和金鑰組的腳本可以向頁面註冊。 嘗試註冊已註冊的腳本並不會建立腳本的複本。

IsClientScriptBlockRegistered呼叫 方法來判斷是否已註冊具有指定索引鍵和類型組的用戶端腳本,並避免不必要地嘗試新增腳本。

在這個方法的多載中 RegisterClientScriptBlock ,您必須確定 參數中 script 提供的腳本會包裝在專案區塊中 <script>

方法會將 RegisterClientScriptBlock 腳本區塊新增至轉譯頁面頂端。 腳本區塊不保證會依註冊的順序輸出。 如果腳本區塊的順序很重要,請使用 StringBuilder 物件將腳本收集在單一字串中,然後在單一用戶端腳本區塊中註冊它們。

另請參閱

適用於

RegisterClientScriptBlock(Type, String, String, Boolean)

使用型別、索引鍵、指令碼常值,以及表示是否加入指令碼標記的布林值,向 Page 物件註冊用戶端指令碼。

public:
 void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags);
member this.RegisterClientScriptBlock : Type * string * string * bool -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String, addScriptTags As Boolean)

參數

type
Type

要註冊的用戶端指令碼型別。

key
String

要註冊的用戶端指令碼索引鍵。

script
String

要註冊的用戶端指令碼常值。

addScriptTags
Boolean

表示是否加入指令碼標記的布林值。

例外狀況

用戶端指令碼區塊型別為 null

範例

下列程式碼範例示範 如何使用 RegisterClientScriptBlock 方法。 請注意,參數 addScriptTags 設定 true 為 ,因此開頭和結尾腳本標記不會包含在 參數中 script

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Define the name and type of the client scripts on the page.
    Dim csname1 As String = "PopupScript"
    Dim csname2 As String = "ButtonClickScript"
    Dim cstype As Type = Me.GetType()
    
    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript

    ' Check to see if the startup script is already registered.
    If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
      
      Dim cstext1 As String = "alert('Hello World');"
      cs.RegisterStartupScript(cstype, csname1, cstext1, True)
      
    End If
    
    ' Check to see if the client script is already registered.
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then
      
      Dim cstext2 As New StringBuilder()
            cstext2.Append("<script type=""text/javascript""> function DoClick() {")
      cstext2.Append("Form1.Message.value='Text from client script.'} </")
      cstext2.Append("script>")
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)
      
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

備註

用戶端腳本是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。 只有一個具有指定類型和金鑰組的腳本可以向頁面註冊。 嘗試註冊已註冊的腳本並不會建立腳本的複本。

IsClientScriptBlockRegistered呼叫 方法,以判斷是否已註冊具有指定索引鍵和類型組的用戶端腳本。 這可讓您避免不必要地嘗試新增腳本。

在這個方法的多 RegisterClientScriptBlock 載中,您可以使用 參數來指出參數中 script 提供的腳本是否以 <script>addScriptTags 元素區塊包裝。 設定 addScriptTagstrue 表示會自動新增腳本標記。

方法會將 RegisterClientScriptBlock 腳本區塊新增至轉譯頁面頂端。 腳本區塊不保證會依註冊的順序輸出。 如果腳本區塊的順序很重要,請使用 StringBuilder 物件將腳本收集在單一字串中,然後在單一用戶端腳本區塊中註冊它們。

另請參閱

適用於