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

定義

透過型別、鍵和腳本字面值, 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 資訊,請參閱 萬維網聯盟(W3C)網站

適用於

另請參閱