Follow Us

Skip Navigation LinksHome > Articles > Asp.net > Stop post back from the ASP.NET button control.

Stop post back from the ASP.NET button control.

Explains how to control the post back from ASP.NET server button through java script

By Pankaj   On   Wednesday, 17 December 2008

Page Views : 31746   |   Technologies : Asp.net

Rating : Rated :
0
| More..

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.

 

To avoid the post back handle the OnClientClick event call the javascript function which retuns true on successful validation otherwise false. If OnClientClick gets true from the script function then it would raise the OnClick event, otherwise it would not.

 

Button in aspx page would look like:

<asp:Button ID="btnValidate" runat="server" Text="Click Here!!" OnClientClick=" return ValidateEmail()" OnClick ="btnValidate_Click"/>

 

javascript method would look like:

    function ValidateEmail()

    {   

         var x = document.getElementById('txtemail');

        

         var val = x.value;

         if(val == "")

         {

           alert("Please enter your email id");

           x.focus();

           return false;

         }

            return true;

    }

 

 

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.

 


Keywords :
Tags :
Rate This Article :

Comments :

# 1 Annonymous Wrote on 01/05/2009


i was just calling the function not using the return keyword now its working fine....thanks



# 2 Annonymous Wrote on 01/08/2009


This was exactly what I was looking for. Great work! Thanks!



# 3 Annonymous Wrote on 01/14/2009


It didnt work for me. OnClick event always fires, doesnt matter wheter it returns false or true from OnClientClick event. (VS 2008, .NET 3.5)



# 4 Annonymous Wrote on 01/14/2009


this post has the attached source code in Visual studio 2008 and .NET framework 3.5. Could you verify your code with the article code, otherwise you could send your sample application to me to try? -Pankaj



# 5 karthik Wrote on 02/02/2009


Please give the normal code with out java script..



# 6 Annonymous Wrote on 04/23/2009


it did not work for me. OnClick event always fires. whether it return true or false.



# 7 Annonymous Wrote on 04/24/2009


Please check OnClientClick in the .aspx file where you are decalring the button. OnClientClick shoule be OnClientClick=" return ValidateEmail()".



# 8 Annonymous Wrote on 06/04/2009


We have this problem. There is a code in VB in ASP.net. this page has certain drop downs where you select values and the corresponding dropdowns gets filled with values as per the last selection. Everytime we select a value from the DropDown the whole page gets refreshed. Is there a way where we can use a JavaScript code in this VB code to stop the page refresh (postback). This is primarily targeted to make the page run faster. I am still trying to figure out how to do this. Thanks, Joy



# 9 Annonymous Wrote on 08/08/2009


very good



# 10 Annonymous Wrote on 10/20/2009


Very nice site!




blog comments powered by Disqus
User Login
Username :
Password :
Register Login

Forgot Password


Related Articles