ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) Metoda

Definicja

Rejestruje instrukcję OnSubmit z Page obiektem przy użyciu typu, klucza i literału skryptu. Instrukcja jest wykonywana po przesłaniu 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)

Parametry

type
Type

Typ instrukcji OnSubmit do zarejestrowania.

key
String

Klucz instrukcji OnSubmit do zarejestrowania.

script
String

Literał skryptu instrukcji OnSubmit do zarejestrowania.

Wyjątki

type to null.

Przykłady

W poniższym przykładzie kodu pokazano użycie RegisterOnSubmitStatement metody .

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

Uwagi

Instrukcja OnSubmit jest jednoznacznie identyfikowana przez jego klucz i jego typ. Instrukcje z tym samym kluczem i typem są traktowane jako duplikaty. Na stronie można zarejestrować tylko jedną instrukcję o danym typie i parze kluczy. Próba zarejestrowania instrukcji, która jest już zarejestrowana, nie spowoduje utworzenia duplikatu instrukcji.

Wywołaj metodę , IsOnSubmitStatementRegistered aby określić, czy instrukcja OnSubmit jest już zarejestrowana przy użyciu danego klucza i pary typów, i unikaj niepotrzebnego próby dodania skryptu.

Parametr script RegisterOnSubmitStatement metody może zawierać wiele poleceń skryptu, o ile są one prawidłowo rozdzielane średnikami (;).

Dodaje RegisterOnSubmitStatement skrypt, który jest wykonywany przed przesłaniem strony i daje możliwość anulowania przesyłania.

Aby uzyskać więcej informacji na temat formularzy HTML i atrybutuOnSubmit, zobacz witrynę internetową World Wide Web Consortium (W3C).

Dotyczy

Zobacz też