This section will discuss the implementation of
ClientScriptManager.RegisterOnSubmitStatement() of ClientScriptManager class.
Sometimes, we will require getting confirmation from the user before submitting
the form to server. For example, in an input form we would like to get the
confirmation from the users before storing it to the database whose validity
cannot be determined through validation functions. This method can be used in
such situations to provide confirmation dialogs. This method registers scripts
which will be executed at the time of submit click of a page.
Listing 9 - Syntax
public void RegisterOnSubmitStatement (
Type type,
string key,
string script
)
Usage
Placing this code on page load makes the script fire on
every submit click of the webform.
Listing 10 - RegisterOnSubmitStatement
if (!script.IsClientScriptBlockRegistered(this.GetType(), "SubmitScript"))
{
script.RegisterOnSubmitStatement(this.GetType(), "SubmitScript",
"alert('Submit Clicked')");
}
Consider the code below.
Listing 10 - RegisterOnSubmitStatement Example
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlockRegistered(this.GetType(), "SubmitScript"))
{
script.RegisterOnSubmitStatement(this.GetType(), "SubmitScript", "return
confirm('Are you sure to continue')");
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("Form is Submitted.");
}
Executing the above code will bring a webform. Clicking
Submit will bring a confirm box like the following figure.
Figure 3 - RegisterOnSubmitStatement Output 1
Clicking Cancel will not execute the submit click event,
where clicking OK will execute the event and the output will be like Figure 4.
Figure 4 - RegisterOnSubmitStatement Output 2