ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) 方法

定義

使用類型、索引鍵和指令碼常值 (Literal),向 Page 物件註冊 OnSubmit 陳述式。 當送出 HtmlForm 時,會執行該陳述式。

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

參數

type
Type

要註冊的 OnSubmit 陳述式類型。

key
String

要註冊的 OnSubmit 陳述式索引鍵。

script
String

要註冊的 OnSubmit 陳述式指令碼常值。

例外狀況

typenull

範例

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

<%@ 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 = "OnSubmitScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the OnSubmit statement is already registered.
    if (!cs.IsOnSubmitStatementRegistered(cstype, csname))
    {
      String cstext = "document.write('Text from OnSubmit statement');";
      cs.RegisterOnSubmitStatement(cstype, csname, cstext);
    }

  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     <input type="submit"
            value="Submit" />
     </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 script on the page.
    Dim csname As String = "OnSubmitScript"
    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 OnSubmit statement is already registered.
    If (Not cs.IsOnSubmitStatementRegistered(cstype, csname)) Then
      
      Dim cstext As String = "document.write('Text from OnSubmit statement.');"
      cs.RegisterOnSubmitStatement(cstype, csname, cstext)
      
    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="submit"
            value="Submit" />
     </form>
  </body>
</html>

備註

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

IsOnSubmitStatementRegistered呼叫 方法來判斷 OnSubmit 語句是否已向指定的索引鍵和類型組註冊,並避免不必要地嘗試新增腳本。

script方法的 RegisterOnSubmitStatement 參數可以包含多個指令碼命令,只要它們以分號 (;) 正確分隔。

RegisterOnSubmitStatement 新增在提交頁面之前執行的腳本,並讓您有機會取消提交。

如需 HTML 表單和 OnSubmit 屬性的詳細資訊,請參閱 World Wide Web Consortium (W3C) 網站

適用於

另請參閱