Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hii,

My requirement is .. if page try to navigate through different page .. I want to dislay an message .. please save data .. on that there will be do buttons on confirm box .. if he click on save .. i want to fire save click event ... and redirect to next page ..

But the problem is when i try to fire this event ... because it was enetered into onbefore unload .. i am unbale to catch that button id ... .. How can i do that ???
JavaScript
// $(window).bind("beforeunload", function () {
// document.getElementById('<%=hdnBack.ClientID %>').value = "";
// debugger;
// if (confirm("If you leave this screen you may lose any unsaved data") == true) {
// var mode = '<%= Request.QueryString["mode"] %>';
 
// if (mode == 'edit') {
// _doPostBack("<%= btnUpdate_Click.UniqueID %>", "OnClick");
//// __doPostBack('btnUpdate_Click', 'OnClick');
// // $('ContentPlaceHolder1_btnUpdate').trigger('click');
// //document.getElementById('<%=btnUpdate_Click.ClientID%>').click;
//// var Update = document.getElementById('<%=btnUpdate_Click.ClientID%>');
//// Update.click;
// }
// else {
// document.getElementById("btnSave_Click").click();
// }
// return true;
// }
// else {
// return true;
// }
// })
Posted
Updated 15-Oct-13 5:33am
v4
Comments
Sergey Alexandrovich Kryukov 15-Oct-13 11:41am    
Why doing so? And why showing commented-out code?
—SA

1 solution

The whole approach is wrong. You don't need to "fire button click event" without clicking on the button, you simply need to have the same action "as if button is clicked". You need the effect of click, not the click itself. So, what's the problem? Do exactly this. You should have some function you call from the click handle. Have it as a named explicitly written function in your script. Call it from onclick handler and also, from your onbeforeunload handler. As simple as that.

—SA
 
Share this answer
 
Comments
Torakami 17-Oct-13 1:34am    
Dude.. firstly when we entered into onberforeunload .. even if we pass click event from that .. it wont work .. the page gets disposed till that time
Sergey Alexandrovich Kryukov 17-Oct-13 1:58am    
It should not. This event worked for me perfectly. I think it's not related to your main question, which I answered in full.
If you have some problem with using onbeforeunload, perhaps you can create a short but comprehensive code sample manifesting your problem and ask to help with it...
—SA
Torakami 17-Oct-13 2:39am    
I have already posted the code brother... plz check that ... I had written one script
Sergey Alexandrovich Kryukov 17-Oct-13 3:08am    
But it's commented out :-)
—SA
Torakami 17-Oct-13 5:49am    
I gave the code .. it was not working in my project so i commented it .. but that wthat ws the code i tried

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900