Page.RegisterClientScriptBlock(String, String) Page.RegisterClientScriptBlock(String, String) Page.RegisterClientScriptBlock(String, String) Page.RegisterClientScriptBlock(String, String) Method

定義

警告

此 API 現已淘汰。

發出用戶端指令碼區塊給回應。Emits client-side script blocks to the response.

public:
 virtual void RegisterClientScriptBlock(System::String ^ key, System::String ^ script);
[System.Obsolete("The recommended alternative is ClientScript.RegisterClientScriptBlock(Type type, string key, string script). http://go.microsoft.com/fwlink/?linkid=14202")]
public virtual void RegisterClientScriptBlock (string key, string script);
abstract member RegisterClientScriptBlock : string * string -> unit
override this.RegisterClientScriptBlock : string * string -> unit
Public Overridable Sub RegisterClientScriptBlock (key As String, script As String)

參數

key
String String String String

識別指令碼區塊的唯一索引鍵。Unique key that identifies a script block.

script
String String String String

傳送到用戶端的指令碼內容。Content of script that is sent to the client.

範例

下列程式碼範例示範如何使用RegisterClientScriptBlock方法搭配IsClientScriptBlockRegistered方法。The following code example demonstrates the use of the RegisterClientScriptBlock method in conjunction with the IsClientScriptBlockRegistered method. 如果在程式碼宣告區塊 ECMAScript 具有尚未註冊,由IsClientScriptBlockRegistered,則RegisterClientScriptBlock進行呼叫。If the ECMAScript in the code declaration block has not already been registered, as determined by IsClientScriptBlockRegistered, the RegisterClientScriptBlock call is made.

<%@ 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)
  {
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";

    if (!IsClientScriptBlockRegistered(csname1))
    {
        String cstext1 = "<script type=\"text/javascript\">" +
            "alert('Hello World');</" + "script>";
        RegisterStartupScript(csname1, cstext1);
    }

    if (!IsClientScriptBlockRegistered(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>");
      RegisterClientScriptBlock(csname2, cstext2.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">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim csname1 As String = "PopupScript"

        Dim csname2 As String = "ButtonClickScript"
    
        If Not IsClientScriptBlockRegistered(csname1) Then
            Dim cstext1 As String = "<script type=""text/javascript"">" & _
                "alert('Hello World');</" & "script>"
            RegisterStartupScript(csname1, cstext1)
        End If
    
        If Not IsClientScriptBlockRegistered(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>")
            RegisterClientScriptBlock(csname2, cstext2.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>

備註

用戶端指令碼,就會發出後方的開頭標記Page物件的<form runat= server>項目。The client-side script is emitted just after the opening tag of the Page object's <form runat= server> element. 務必包含左右<script>項目周圍的指令碼區塊中指定的字串script參數。Be sure to include opening and closing <script> elements around the script block string specified in the script parameter.

由於這個方法會使用索引鍵來識別指令碼區塊,指令碼區塊並沒有發出至輸出資料流不同的伺服器控制項執行個體要求每一次。Because this method uses a key to identify the script block, the script block does not have to be emitted to the output stream each time it is requested by a different server control instance. 使用金鑰時,也會降低互相干擾的不同控制項的指令碼區塊的可能性。Using a key also decreases the likelihood of different controls' script blocks interfering with each other.

任何指令碼具有相同的區塊key參數值會被視為重複。Any script blocks with the same key parameter values are considered duplicates.

注意

請記得 HTML 註解前後加上標記您的指令碼,讓它將無法呈現如果要求的瀏覽器不支援指令碼。Remember to include HTML comment tags around your script so that it will not be rendered if the requesting browser does not support scripts.

RegisterClientScriptBlock 方法已被取代。The RegisterClientScriptBlock method has been deprecated. 請改用 RegisterClientScriptBlock 類別中的 ClientScriptManager 方法。Use the RegisterClientScriptBlock method in the ClientScriptManager class instead.

適用於