[Sample of Apr 30th] Run scripts after server side validation in ASP.NET

 

Homepage image
Sample of the Day RSS Feed

Sample Downloads: https://code.msdn.microsoft.com/AddDynamicallyControlstoGri-d74fe84f  

Today’s sample demonstrates how to run client script after server side validation. Normally, client script is placed to validate user inputs at client side. After this validation is successful, server side validation is then performed. But sometimes, after server side validation, ​ we'll need to run client script to perform tasks that only achievable by javascript, for example, showing a dialog box, scrolling the page to a designated place, etc. .

The sample was written by our star sample writer: Arwind Gao.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.

Introduction

The sample code demonstrates how to run client script after server side validation. Normally, client script is placed to validate user inputs at client side. After this validation is successful, server side validation is then performed. But sometimes, after server side validation, ​ we'll need to run client script to perform tasks that only achievable by javascript, for example, showing a dialog box, scrolling the page to a designated place, etc.

Running the Sample

Please follow these demonstration steps below.

Step 1: Open the CSASPNETRunScriptsAfterServerSideValidation.sln.

Step 2: Right click Default.aspx and choose "View in Browser".

Step 3: Enter an email in the textbox and click the button to validate.

 

Using the Code

Step 1. Create a C# "ASP.NET Empty Web Application" in Visual Studio 2010 or Visual Web Developer 2010. Name it as "CSASPNETRunScriptsAfterServerSideValidation".

Step 2. Add a TextBox Control, a Button, a RequiredFieldValidator and a CustomValidator to the Dafault page.The add a div in the bottom of the form for run scripts after server side validation.

Step 3. In the Page_Load event, Page object gets checked to see if the value of the input element is valid. If the value is valid, then register a script to run at client side. This script is used to perform client side tasks.

First, get the value of the input.

Second, declare the pattern to validate the email.

Finally, according to the results of the verification decision as to whether to register a script to run at client side or not.

Step 4. Build the application and you can debug it.

ClientScriptManager.RegisterStartupScript Method