Reverse order of operations for OnClick and OnClientClick? - asp.net

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.

Related

disabling asp button onclick prevents event from firing

I'm trying to disable an asp button when it is clicked on the client side.
<asp:Button runat="server" ID="btnSubmit" Text="Save" class="Button" OnClick="btnSubmit_Click" CausesValidation="false"></asp:Button>
<script type="text/javascript">
$("input[type=submit],button").click(function (){$(this).attr("disabled", "disabled");});
</script>
when the button is clicked it does post back to the page_load , but it won't go to the btnSubmit_Click method. If I remove the jquery that disables the button it makes it from the page_load to the btnSubmit_Click method.
It seems when the button is disabled from the jquery it is no longer able to wire up to its event. Anyone have any ideas how I can disable the button client side, and still make it to the specific onclick event method?
This is resolved (Yuriy answered), the issue was that I needed to put the disabled code within the form.submit client side code. If not the disabled button was not firing its click event, yet it was still posting back, just not going to the onclick method.
solution:
$('#Form1').submit(function(){ $("input[type=submit],button").click(function (){$(this).attr("disabled", "disabled");});

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.

UpdatePanel update without trigger button

I have an UpdatePanel with ContentTemplate specified. When page loads, user can do some AJAX work in other part of the page. Then, after that work is finished, I would like to update only content inside UpdatePanel, but without pressing any buttons etc. I should be done automatically using JavaScript when previously started AJAX work finishes. How to do it without manual clicking on the trigger button?
EDIT:
Ok, I've followed that _doPostBack rule, and whole page is posted.
<asp:UpdatePanel ID="panelAttachments" runat="server">
<ContentTemplate>
........
</ContentTemplate>
</asp:UpdatePanel>
<input type="text" name="test" onchange="__doPostBack('<%=panelAttachments.UniqueID %>',''); return false;" />
</td>
Thanks, Pawel
To refresh an update panel from javascript:
__doPostBack(updatePanelUniqueID,'');
The first parameter is the UniqueID (not CientID) of the UpdatePanel.The 2nd parameter is optional arguments you can pass which will be available to your server code. Both are stored in hidden form fields by ASP.NET, you can access them in codebehind:
Request.Form["__EVENTTARGET"];
Request.Form["__EVENTARGUMENT"];
But if you just want to refresh a panel and don't need to pass any additional info from the client, you can ignore then 2nd argument.
If you look at the HTML generated by ASP.NET for an async postback control, you'll see it's exactly this.

Using jQuery with ASP.NET web forms on postback

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;
}

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