ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) メソッド

定義

OnSubmit ステートメントを、型、キー、スクリプト リテラルを使用して、 Page オブジェクトに登録します。 ステートメントは、 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 ステートメントは、そのキーとその型によって一意に識別されます。 同じキーと型を持つステートメントは重複と見なされます。 特定の型とキーのペアを持つステートメントを 1 つだけページに登録できます。 既に登録されているステートメントを登録しようとしても、ステートメントの重複は作成されません。

IsOnSubmitStatementRegistered メソッドを呼び出して、OnSubmit ステートメントが既に特定のキーと型のペアに登録されているかどうかを判断し、スクリプトの追加を不必要に試みないようにします。

RegisterOnSubmitStatement メソッドの script パラメーターには、セミコロン (;)) で適切に区切られている限り、複数のスクリプト コマンドを含めることができます。

RegisterOnSubmitStatementは、ページが送信される前に実行されるスクリプトを追加し、申請を取り消す機会を提供します。

HTML フォームと OnSubmit 属性の詳細については、 World Wide Web Consortium (W3C) Web サイトを参照してください。

適用対象

こちらもご覧ください