Stop Post back during the validation in the asp.net button control:
Button control in ASP.NET is a server control, which does the post back on every click. In some practical scenarios some time we want to send the user inputs to server for processing after making sure that information is correct. We would learn how to control the post back of the asp.net button control on the web page in this article without using the any hidden control.
In the default.aspx sample page, I have taken txtemail text box and an asp.net button called btnValidate. If user has not specified the emailed in the txtemail text box, show error to the user and don’t post back the page to the server. If user has entered something in the txtemail, process the event and go to the default2.aspx page.
Button in aspx page would look like:
<asp:Button ID="btnValidate" runat="server" Text="Click Here!!" OnClientClick=" return ValidateEmail()" OnClick ="btnValidate_Click"/>
var x = document.getElementById('txtemail');
var val = x.value;
if(val == "")
alert("Please enter your email id");
Summary: In this article we have seen that without using any hidden button, how we can control the post back on the asp.net button click.
NOTE: Please download the attached code of visual stuido 2008, .NET Framework 3.5.