Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Postback causing <BODY OnUnload="..."> to fire

Status
Not open for further replies.

jmikow

Programmer
Mar 27, 2003
114
US
I have some Javascript that executes when the page is closed by clicking the "X" in the browser. It is tied to the <BODY OnUnload="..."> event.

I have noticed that when I click a button on the page that causes a postback and runs server-side code that it triggers the "OnUnload" event of the BODY tag.

Is there any way in the Javascript to detect if the BODY unload is because of a postback or because the window is closing?

Any ideas or suggestions are welcome!

Thanks,

Josh
 
You can see from the example that the hidden form values can be hard to track. I have two button, I clicked butAdd.

__EVENTTARGET=
__EVENTARGUMENT=
ctrl_adm_clients1:butAdd= Add Client
ctrl_adm_clients1:drpCounselors=

It would be better to put this in your form.
<form runat=server onsubmit="JavaScriptVarIsPostBack=true;return true;">
 
RTomes,

I'm fairly new to ASP.Net and haven't gotten too deep other than what VS provides.

Would I set the value of a hidden textbox and then check that value to determine if it is a postback or window close? I've tried this a bit and haven't been able to get it to be accurate.

Thanks for the tip. I didn't realize I could do javascript on form submit. I'm going to work with that also.

Thanks,

Josh
 
What is was showing above was to use the onsubmit. Trying to query all the form elements is a daunting task. Use onSubmit().


<head>
<script>
var IsPostBack=false
function CheckIt(){
if(IsPostBack){
//Do what ever you want it is a post back....
}else{
//Do what you want if it is not....
}//end if
}
</script>
</head>
<body onunload=CheckIt() >

<form runat=server onsubmit="IsPostBack=true;return true;">
 
RTomes,

I was able to get it to work using the OnSubmit() event since that fires before the OnUnload() event.

I set the variable in the OnSubmit and check its value in the OnUnload. Depending on the value I decide what to do when the page is unloaded.

Thanks for the tip and example!

Josh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top