Saturday, December 10, 2011

ClientScriptManager.RegisterOnSubmitStatement Method

The RegisterOnSubmitStatement adds a script that is executed before the page is submitted and gives you an opportunity to cancel the submission.
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.
Registers an OnSubmit statement with the Page object,the statement executes when the HtmlForm is submitted.

In .aspx,
<form id="form1" runat="server">
<asp:button onclick="Button1_Click" id="Button1" runat="server" text="Submit">

In code-behind,
protected 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 = "return confirm('Are you sure go away from this page?');";
cs.RegisterOnSubmitStatement(cstype, csname, cstext);


protected void Button1_Click(object sender, EventArgs e)

That's it. :)

No comments:

Post a Comment