Page.RegisterStartupScript(String, String) Page.RegisterStartupScript(String, String) Page.RegisterStartupScript(String, String) Page.RegisterStartupScript(String, String) Method

定義

警告

此 API 現已淘汰。

在頁面回應中發出用戶端指令碼區塊。Emits a client-side script block in the page response.

public:
 virtual void RegisterStartupScript(System::String ^ key, System::String ^ script);
[System.Obsolete("The recommended alternative is ClientScript.RegisterStartupScript(Type type, string key, string script). http://go.microsoft.com/fwlink/?linkid=14202")]
public virtual void RegisterStartupScript (string key, string script);
abstract member RegisterStartupScript : string * string -> unit
override this.RegisterStartupScript : string * string -> unit
Public Overridable Sub RegisterStartupScript (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 will be sent to the client.

範例

下列程式碼範例示範如何使用RegisterStartupScript方法搭配IsStartupScriptRegistered方法。The following code example demonstrates the use of the RegisterStartupScript method in conjunction with the IsStartupScriptRegistered method. 如果在程式碼宣告區塊寫入 ECMAScript 具有尚未註冊,由IsStartupScriptRegistered方法,則會顯示RegisterStartupScript進行呼叫。If the ECMAScript written in the code declaration block has not already been registered, as determined by the IsStartupScriptRegistered method, then a RegisterStartupScript 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>

備註

類似於RegisterClientScriptBlock方法中,RegisterStartupScript方法會發出目的結尾標記之前的指令碼Page物件的<form runat= server>項目。Similar to the RegisterClientScriptBlock method, the RegisterStartupScript method emits the script just before the closing 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

任何指令碼具有相同的區塊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.

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

適用於

另請參閱