PostbackUrl property issues after preventing user from posting asp.net page

N

noneya22

I'm using asp.net 2.0. I have a page that has a save button and a
cancel button along with a text field. All controls are asp.net
server controls. I have JavaScript that prompts the user if he has
maninpulated data in the text field and then tries to leave the page.
The purpose is to prevent the user from accidentally losing his
changes. This JavaScript code that accomplishes this is based upon
the code found at http://aspnet.4guysfromrolla.com/articles/101304-1.aspx.
Basically it makes use of the window.onbeforeunload event to prevent
navigation from the current page.

The cancel button has the PostbackUrl property set. The reason for
this is to navigate to the page the user was at before the current
page thus cancelling the operation. I understand that there are other
ways of achieving the scenario I have described here (i.e. hyperlink,
postback with a server redirect, etc.). I'm trying to keep this
example simple. Also, the save button does not have the PostbackUrl
property set as it is intended to cause a postback to the same page
and process the data.

So, here is the problem scenario. If the user manipulate's the text
in the text field and clicks the cancel button, he receives a prompt
that allows him to stop the current operation and stay on the current
page. What is happening is once this occurs, every other input
control on the form now will post to the URL set on the cancel
button's PostbackUrl property. Say the user now clicks the Save
button, they will not post back to the same page, and thus save the
data, they will post to the page specified by the cancel button's
PostbackUrl property.

I believe this has something to do with the code executed by
WebForm_DoPostBackWithOptions, but I am not sure. It seems as if the
form's target gets reset to the postback url, maybe by this code, and
then you're stuck. Any suggestions on why this is occurring and how
to prevent it would be much appreciated.

Thanks!
 
B

bruce barker

you are correct, the target url is changed then form.submit() is called,
which triggers your unload event (which is actually trigged by the start
of the load of the postback).

in RegisterStartupScript, save the original target url, and restore in
your unload event if canceled.

theForm.origTarget = theForm.target;


-- bruce (sqlwork.com)
 
N

noneya22

you are correct, the target url is changed then form.submit() is called,
which triggers your unload event (which is actually trigged by the start
of the load of the postback).

in RegisterStartupScript, save the original target url, and restore in
your unload event if canceled.

theForm.origTarget = theForm.target;

-- bruce (sqlwork.com)









- Show quoted text -

I was nervous about doing this type of "override" of asp.net's
generated code/js. But you were right and this seems to work as far
as I can tell. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

PostBackURL 4
PostBackURL 1
PostbackUrl 3
ListBox and PostBackUrl event 3
PostbackURL not working 1
PostBackURL problem 1
PostBackURL, Server.Transfer, and PreviousPage 1
ImageButon: PostBackURL 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top