Prevent page refresh in ASP.NET - asp.net

I have the following code in my aspx file:
<button type="button" id="btnAskQuestion" runat="server" onserverclick="btnAskQuestion_Click">Ask Question</button>
I've tried every combination of onclick="return false;" and onclick="preventDefault()" I can think of, including putting them in the javascript function that gets called. Everything I try has one of two results: either I get a postback, or the server side code (btnAskQuestion_Click) never executes.
Any idea what I might be doing wrong?

You cannot execute server-side code this way, using onserverclick causes postback.
If you wish to prevent full page refresh and still execute server-side code, you have to call a client-side JS function via onclick and execute an AJAX call from there.
Another alternative is to use your button as a trigger for UpdatePanel - this way only partial postback will be performed.

Try using the property UseSubmitBehavior="false" in the button markup.
or you can use a "trick" :
Markup
<button onclick="myFunction()">Click Me!</button>
<div style="display:none">
<asp:Button runat="server" id="btnButton" .../>
</div>
js
function myFunction()
{
if (true statement)
$("[id$=btnButton]").click();
else
alert("false");
}
What this does is that you handle stuff with normal markup and do the logic using js. And you can trigger a click of the button that do something in the server.

There're OnClick, that fires on server and OnClientClick that fires on client browser. You should do this:
<asp:Button ID="btnAskQuestion" runat="server"
OnClick="btnAskQuestion_Click"
OnClientClick="return myfunction();">Ask Question</asp:button>
If myFunction returns true, then you will have a postback to the server.

My answer is appropriate only for ASP:Button, not the button control you are working with. Given the choice, I'd switch to ASP:Button.
You're looking for OnClientClick. If you put your JavaScript code there, it will kill the PostBack before it can hit the server.
On the other hand, if you're looking to execute server code without a PostBack, that's impossible. The PostBack is what triggers the server to act.

Related

Can you place an if statement in .ascx layout?

I'm new with working with ASP.net and .ascx, and now I've seen a button that calls a method by 'OnClientClick' So the code looks like this:
<asp:Button Text="Save" OnClick="BtnSave_Click" OnClientClick="isBusy();"/>
Now I want that the method only calls at certain definitions declared in the same .ascx file. And thus I thought that an if-statement inside the ascx would work. So I've already tried attempts like OnClientClick="if(Text.Length <= maxlength) { isBusy(); } but that caused the line to not respond at all.
Currently I'm wondering if an if-statement in this situation is actually possible.
it is doable. but you need to make sure your js is correct.
in your question, what is Text.Lengh ?
whatever, if you want block the server side postback, then return false in your onclick JS, that will completely mute the postback event
for example
<asp:button runat="server" onclientclick="return false;" />
this button will never post back

Jquery UI Button Server + Client Click Handlers

I am trying to write a simple HTML form using asp.net and Jquery UI but am running into a problem when trying to process click event handlers on a button within this form. I was tryign to use OnClientClick and OnClick but once the clientside method gets accessed and returns the boolean the server side method is not called accordingly( not called at all actually)
Linky to code since I could not get the code tags to work properly: http://pastebin.com/LZNMqASt
I found the problem, Actually you are displaying "div#loginForm" element in to the dialog and its not taking the form element.
Put form element inside of "div#loginForm" container and the problem will be fixed.
For some reason the return type of the javascript method was not being accepted as a valid boolean. The below solution fixes the OnClientClick event
<asp:Button runat="server" ID="btnLogin" Text="Login" OnClick="btnLogin_OnClick"
OnClientClick="if(ValidateLoginForm() != true) return(false);" UseSubmitBehavior="False" />

How to prevent PostBack on the client side?

I have some validation JS code on client, that must be executed befor PostBack.
If this validation code return 'false', postback is needless.
How it can be disabled?
Remember that the real validation should always happen on the server. Anything you do client-side is merely an optimization to save a few http round trips.
The easiest way to keep your client side and server-side validation in sync with ASP.Net is to use the validation controls. The validation controls will do both client side and server side validation, in such a way that if validation fails on the client it never posts to the server.
If you want to do something that's not covered by the standard validation controls, you should either use a CustomValidator or inherit your own control from BaseValidator.
Set the OnClientClick='YourJSValidationFunction' on your ASP button.
Then have the YourJSValidationFunction return true or false.
False will prevent postback
Example:
http://vijaymodi.wordpress.com/2007/06/08/button-onclick-and-onclientclick-2/
If the postback is being triggered by a button then you can do something like this:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return IsValid();" />
If the IsValid function returns false then the postback will be prevented.
If you want to catch all postbacks regardless of which control triggers it then you can use <form id="form1" runat="server" onsubmit="return IsValid();">
What do you use: some validator or some button with onclick event?
If you have
<input type="button" id="btnID" runat="server" onclick="CheckValid();"/>
function CheckValid()
{
if(!IsValid) return false;//then no post back occer
}
Depending on the validation you're attempting, you may also be able to use the CustomValidator control. This would also allow you to easily implement your validation logic on the server side.

Reverse order of operations for OnClick and OnClientClick?

I have some simple javascript that I'd like to run when a button is clicked, but I also want some postback action to occur on the server. The logical code for this looks like this:
<asp:Button ID="btnOK" runat="server" Text="Save Changes" OnClientClick="UpdateParent();" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="window.close();" />
<script language="javascript" type="text/javascript">
function UpdateParent()
{
window.opener.document.location.reload(true); // or should we postback instead?
window.close();
}
</script>
Basically, a popup window should refresh its parent and then close itself. However... if I call window.close(), the postback does not occur and the button handler is not called. But obviously OnClientClick is called before the postback happens. Am I going to have to emit this javascript in the button handler and run it when the page loads after postback? If so, what is the proper way to do this these days for ASP.NET 2.0?
It's a shame that the code above doesn't work as it's elegantly simple and straightforward.
You have to do the postback before closing the window. Also you want to do the postback before refreshing the parent window, as I guess that the reason to refresh the window is to display the information that you are about to save.
Use the RegisterStartupScript in the ClientScript object to run the code after postback:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.reload(true);window.close();", true);
However, if the parent page is a result of a postback, this would cause a dialog window in the browser informing the user that a post request is needed to reload the page. To avoid this you would have to do something like calling a function in the parent page that could do a postback to update the page.

ASP.NET CheckBox disabling postback with javascript

I'm trying to wire up a CheckBox to handle an event when check/unchecked. If the user has JavaScript enabled, use that, otherwise use a postback.
Here is my code:
<asp:CheckBox ID="ApplicationInProcessCheckBox" runat="server"
Text="Application In Process" AutoPostBack="true"
oncheckedchanged="ApplicationInProcessCheckBox_CheckedChanged"
onclick="return false;" />
The return false in the javascript onclick event is disabling the postback. However, it also won't let the box check or uncheck. (I have more code to add to the javascript event... I just want to get the concept working first).
What am I doing wrong?
I think we can't post back on clicking checkbox without Javascript enabled.

Resources