Using jQuery with ASP.NET web forms on postback - asp.net

I've read plenty of good blog posts on integrating jQuery with ASP.NET web forms to utilize the data access capabilities, but I'm curious to know how easy it would be to use some of jQuery's animation capabilities before a postback. I can think of two good examples:
1) When I click on a asp:button, animate a div, but then do a regular postback when the animation is finished (or complete the animation after the postback?).
2) When I click on a linkbutton in a gridview, fade out the gridview row, then do the regular postback in the linkbutton's onclick event.
I'm not aware of a solution to this. Everything I've tried has either run the postback before the animation even starts, or runs the animation but then you need to execute the postback in JavaScript (yuck).

Yes, of course you can do that.
Say for example we take a button to do some postback,
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
OnClick Event - cause the postback to be processed on the server side.
But if you also define a OnClientClick event and call any JavaScript function with a return value to the call. It will decide the post back after the JavaScript function is executed.
For example,
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="return DoJqueryAnimations();" />
In above example - DoJQueryAnimations() is a JavaScript function. We call it with a "return:".
So when the function is called and decides what to return, the postback will wait for the client function to give the value - true(do postback) / false (no post back).
In the JavaScript function you do the animation and return true.
For example:
function DoJqueryAnimations()
{
//Do some animations here or call other JavaScript function.
return true;
}

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

Prevent page refresh in 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.

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" />

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