confirm messagebox, but after onclick firing

G

Guest

I can create Javascript confirm message boxes during page creation, etc
adding them to the button attributes (many good posts on this!). But how can
I add this event after the button is pressed?
I have created an Is_Dirty routine checking for field changes on my page
then if the user clicks on the "exit" button I check for Is_Dirty = true and
want to ask then "do you want to exit without saving?". It's a code behind
where if the value of Is_Dirty = True then I want to display the messagebox
with return confirm. So if the user clicks "cancel" then they are back on the
form.
I've tried:
1.) But return can not be used in front of confirm, I get an error, but
works without confirm.
Dim popupScript As String = "<script language='javascript'>" & _
"confirm(""Are you sure you want to exit without saving
changes?"");" & _
"</script>"
Response.Write(popupScript)

Any ideas? thanx.
 
D

Dexter

And if you to use at button event,
page.RegisterStartUpScript("confirm","<script>confirm('message');</script>")

Dexter
 
G

Guest

I would try an if statement inside the onclick event of the ok button and for
the true part of the if statement, I would insert the
page.getpostbackeventreference ....
How does that sound?
 
G

Guest

The cancel button is "CausesValidation="False" " so there is no post back. I
do this to handle the page validation, thus, removing it.
So I thought there was a way to put it in the stream somehow? HTML Stream?
Is there another page property to access for this?
As I can insert the confirm but without the return? With the return I get
"return statement out of function" Javascript page error??????

thanx.
 
G

Guest

I'm not sure what you mean by putting "it in the stream". What is it you
would like to write into the HTML stream - the client-script? If so, you can
override the OnRender sub and write to the HTMLTextWriter stream, just make
sure to call the base OnRender also.
 
G

Guest

I'd prefer doing this on the code-behind (VB) as I am trying to utilize the
..NET capability. I love combining the client/server Windows programming
techniques and web browser client development. Ideally I want to program a
web application as close to that structure as possible. I really like the
control programmically over the controls, etc.
There must be a way to force another event after firing? Since the "return"
statement is error'ing, can I add another Javascript piece to handle the
return?
Can we use any VBScript messagebox objects here? That I can send via the
code-behind?
Thanx.
 
G

Guest

Can I do an "if then" off of what was chosen in the Javascript messagebox?
Since I can't use a return, all I really need to do is:

if "ok" then redirect, if "cancel" then return to page (really do nothing).

Dim popupScript As String = "<script language='javascript'>" & _
"confirm(""Are you sure you want to exit without saving
changes?"");" & _
"</script>"

like a Javascript returnValue = [window.]confirm(message)
"Ok" would return true, "cancel" would return false.
How could I check that and do my if then statement?

'check for changes on the form
If f_IsDirty = False Then

Response.Redirect("XXXXXX.aspx?c=" & cx & "p=m")

Else

Dim popupScript As String = "<script language='javascript'>" & _
"confirm(""Are you sure you want to exit without saving
changes?"");" & _
"</script>"
Response.Write(popupScript)

Response.Write("form IS dirty<br>")
If ????????.returnValue = true then
Response.Redirect("XXXXXX.aspx?c=" & cx & "p=m")
Else
'Do nothing thus returning to page
End If


End If
 
E

Eliyahu Goldin

You can provide a hidden <input> element where you can pass a message from
server to client. In client-side onload event for <body> check the value of
the textbox and if it is not empty use it as a text for a confirm message.

Eliyahu
 
G

Guest

I thought I'd try another approach but am stumped once more.

I modified my "dirty" routine to update the button attributes. Thinking that
the button now will have the onclick routine, i.e.:

Public Sub Is_Dirty(ByVal s As Object, ByVal e As EventArgs)

f_IsDirty = True
btnexit.Attributes.Add("onclick", "return confirm(""Are you sure you
want to exit this without saving?"");")

End Sub


But now once the user clicks the exit button it recongizes that it is
"dirty" if a change was made but the new Onclick doesn't register until the
next time the button is pressed again.
So the question is: if in the code behind there is a Sub for the "Click" and
then you add the attribute for that button for the "click" which is done
first? It appears the Sub that the control has as OnClick="_Click"

control:
<asp:ImageButton id="btnexit" CausesValidation="False"
OnClick="btnexit_Click" ImageURL="images/xxxxx/exit_btn.gif" align="left"
Runat="server" />

so if I press the exit button twice then I get the message box???????? I
rem'd out the redirect to show the "form IS dirty" text so then a can press
the button again to get the message dialog.

Sub btnexit_Click(ByVal s As Object, ByVal e As ImageClickEventArgs)

Dim cnumber As String

'Set concern number
cnumber = txtxxxxxxxx.Text

'check for changes on the form
If f_IsDirty = False Then
'redirect back to concern number
Response.Redirect("xxxxxxxx.aspx?c=" & cnumber & "p=m")
Else

'Response.Redirect("xxxxxxxx.aspx?c=" & cnumber & "p=m")
Response.Write("form IS dirty<br>")

End If

End Sub
 
G

Guest

not sure I'm following you here? I can create a textbox, but isn't that the
same as making a value equal to booleen? And I can't do this at "onload"
because I need to check to see if one of the form fields that is required has
changed first. Once changed then dirty = true so then if exit is clicked show
message box.
So weather I use the field or set in the code behind the issue still would
be the message box display and return wouldn't it?
thanx.
 
G

Guest

I figured out that you have to use the autopostback="true" to get the changes
recongized. I don't like the flicker of the page as it posts back to itself
for every field. there must be a better way to do this?????????????
Thanx.
 
E

Eliyahu Goldin

I mean onload event following the postback, after the server side decided
whether to produce the confirmation message or not. If the message should be
produced, the server side will pass the message text (not textbox, it was a
typo, sorry) in the hidden <input> and the client will know whether to call
confirm or not.

Eliyahu
 
G

Guest

Hi,

If you want to go to extreme level, try this.

If you are only using IE, then try XMLHttpRequest Object on client side
javascript. You just pass the value and get the result from the server,
without postback.

Prakash.C
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top