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) 网站

适用于

另请参阅