How to stop a script?

R

Russ

I have a form page with a submit button. When the submit is
successfull (good return from web service), I insert a script to put
up a message box saying "Form submitted".

That works fine, but afterwords, if the user uses a different button
to go to a different page (using Server.Transfer), and then uses the
Back button to return, the message box pops up again, even though
nothing more was submitted.

I think I understand why this is happening, the script was inserted in
the page, and the modified page is reloaded when the back button is
used. So how can I prevent this? Is there some way to remove the
script once it has executed?

What I tried:

The script is inserted as part of a LiteralControl, so I tried
removing the control just before leaving the page:

The script was created this way:

String s = "<script>alert('" + msg + "')</script>";
LiteralControl lc = new LiteralControl (s);
lc.ID = "msgBox1";
Page.Controls.Add (lc);

So I tried removing it like this:

IEnumerator enu = Page.Controls.GetEnumerator ();
while (enu.MoveNext()) {
object obj = enu.Current;
if (obj.GetType ().Equals (typeof (LiteralControl))) {
LiteralControl lc = (LiteralControl) obj;
if (lc.ID == "msgBox1") {
Page.Controls.Remove (lc);
break;
}
}
}

It does not work. The enumerator only finds one LiteralControl, but
its' ID string is null. I tried letting the routine delete it anyway,
but the script still popped up the message when returning to the page.

Thanks for any help, Russ
 
G

Guest

I think you'll have to do the transfer before the posting of the alert.
Also only output the alert if Page.IsPostBack == false

Hope that helps

</script>
 
S

softcoder

I think you'll have to do the transfer before the posting of the alert.
Also only output the alert if Page.IsPostBack == false

Hope that helps
 
B

bloomfield

I think is no much you can do. The script reach the client side and is
cached by the browser. When you hit the back button on the browser it
will just display the old page. Maybe setting some cache options to
force the browser request the page again can resolve this.
 
R

Russ

Hi softcoder. Thanks for the reply. But I think you missed one point.
The message is put up and removed (by the user pressing OK). Then,
sometimes, the user will click a button to go to a new page. So the
messagebox is not showing when he leaves the page. But when he
returns to the page up pops the messagebox.

I have put a breakpoint in the code where the message box script is
inserted to make sure the code was not getting called again somehow,
but that is not the case. So using IsPostBack will not help since the
code is not being executed anyway.

Russ
 

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

Top