ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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 陳述式指令碼常值。
例外狀況
type
為 null
。
範例
下列程式碼範例示範 如何使用 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
屬性的詳細資訊,請參閱 World Wide Web Consortium (W3C) 網站。