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. 如果程式IsClientScriptBlockRegistered代碼宣告區塊中的 ECMAScript 尚未註冊(由決定),則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.

適用於