How to stop the page refresh after click on button in asp.net
Solution 1
Your question says that you are working on client side then use
OnClientClick="course(); return false;"
.
Solution 2
if you want to call javascript method use OnClientclick()
and if you want call both the methods use onclick event to call the code behind method i.e method in aspx.cs and inside use this code
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", course());
Solution 3
asp.net web forms will always do a postback on a runat="server" button click. that is how it reaches the code behind (.cs file). for client side clicks you can either use aspx ajax or jQuery click or javascript onclick functions to prevent postbacks. For this very reason more and more asp.net developers are moving to asp.net mvc.
Try just creating a simple html button and doing and onclick to run you javascript function:
<button onclick="javascript:some_js_function();">Click Me</button>
or jQuery:
<button id="btn">Click Me</button>
in you document.ready:
$('form').on('click', '#btn', function(evt) {
evt.preventDefault();
some_js_function();
});
user3230677
Updated on October 14, 2020Comments
-
user3230677 over 3 years
I have a asp.net page where i am using javascript code in Edit.aspx and declared a method in it add course() as soon as the add course method is completed the control goes to the Button click event which is in Edit.aspx.cs and after the button click is completed the whole page refreshes
-
Nitin Varpe about 10 yearscan u show aspx and javascript code?
-
raza rabbani about 10 yearsif you are not doing any work in Click event on Edit.aspx.cs , just return false on client click of button.
-
Anh Tú about 10 yearstry
OnClientClick="return course();"
incourse()
method must be returntrue
orfalse
and no ajax.
-