CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Home > CodeDigest >  
Technologies
 


Using RegisterOnSubmitStatement() Method of of ClientScriptManager Class
Submitted By Satheesh Babu B
On 4/24/2009 9:54:04 AM
Tags: ASP.Net,ASP.Net 2.0,CodeDigest  

Using RegisterOnSubmitStatement() Method of of ClientScriptManager Class

Read my previous code snippet Using RegisterClientScriptBlock() Methods of ClientScriptManager Class and Using RegisterStartupScript() Methods of ClientScriptManager Class

Moving forward, this little article will help us to use the ClientScriptManager.RegisterOnSubmitStatement() method of ClientScriptManager class.

At times, it is required to get 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.

public void RegisterOnSubmitStatement (
    Type type,
    string key,
    string script
)

Implementation
Placing the below code on page load makes the script fire on every submit click of the webform.

if (!script.IsClientScriptBlockRegistered(this.GetType(), "SubmitScript"))
{
script.RegisterOnSubmitStatement(this.GetType(), "SubmitScript",
"alert('Submit Clicked')");
}

The below code snippet will pop up a confirm box with message "Are you sure to continue" when you try to submit a form.

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.");
}

 

Recent Codes
  • View All Codes..