Editar

Compartir vía


ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) Method

Definition

Registers an OnSubmit statement with the Page object using a type, a key, and a script literal. The statement executes when the HtmlForm is submitted.

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)

Parameters

type
Type

The type of the OnSubmit statement to register.

key
String

The key of the OnSubmit statement to register.

script
String

The script literal of the OnSubmit statement to register.

Exceptions

type is null.

Examples

The following code example demonstrates the use of the RegisterOnSubmitStatement method.

<%@ 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>

Remarks

An OnSubmit statement is uniquely identified by its key and its type. Statements with the same key and type are considered duplicates. Only one statement with a given type and key pair can be registered with the page. Attempting to register a statement that is already registered will not create a duplicate of the statement.

Call the IsOnSubmitStatementRegistered method to determine whether an OnSubmit statement is already registered with a given key and type pair and avoid unnecessarily attempting to add the script.

The script parameter of the RegisterOnSubmitStatement method can contain multiple script commands as long as they are properly delimited with a semicolon (;).

The RegisterOnSubmitStatement adds a script that is executed before the page is submitted and gives you an opportunity to cancel the submission.

For more information on HTML forms and the OnSubmit attribute, see the World Wide Web Consortium (W3C) Web site.

Applies to

See also