ASP.NET Web form controls and HTML Server Control can be made to execute client side script before posting the page back to the server. Here the author has taken an example of a button that, when clicked, calls a function Validate(), which is on the client side first, and then submits the page.
ASP.NET Web form control
Let us start with a button which is an ASP.NET Web form control type:
i.e <asp:button id="acceptButton" runat="server" Text="Submit"
</asp:button>
In the Page_Load Event of this page write the following statement:
acceptButton.Attributes.Add("onclick", "return Validate()")
Here Validate() is a simple function written in JavaScript or VBScript. In this function all client side validations can be done. And when all the validation checks are met, then the function merely returns true to post the page back to the server. Returning false from the Validate() function does not post the ASP.NET page.
HTML Server Control
These controls are similar to the HTML control, However they have an attribute runat = "server", which makes them server controls.
<INPUT id="acceptButton" onclick="return validate();" type="button" value="Submit" name="myButton" runat="server" onserverclick="myButton_ServerClick">
Here we see that this control has two attributes: onClick, which calls
Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.
Hannah Smalltree, Editorial DirectorSource: DotNetExtreme.com
This was first published in January 2003