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 サイトを参照してください。

適用対象

こちらもご覧ください