Javascript Confirm()

S

shil

Hi all,

Can any one direct me in how to use javascript confirm() function when
a condition is met from codebehind. I don't want to attach this to a
button. When I submit the page, I want to check some business rules,
and based on the response from the database, if the response is Yes, I
need to display a confirmation alert to user. When the user clicks ok,
then I should take an action, if not, exit out of the subroutine.

Below is the script I'm using in the Submit_Click() subroutine.

If Trim(Message) <> "" Then
Dim strScript As String = "<script
language=JavaScript>"
strScript += "confirm(""" & Trim(Message) &
""");"
strScript += "</script>"
If (Not
Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript",
strScript)
End If
End If

In the above code, I'm getting a message from the Sql database into
Message variable, and showing that to the user.
But I don't have a way to track what an user have selected from the
confirmation alert box.

Any help is appreciated.
Thanks.
 
L

Laurent Bugnion

Hi,
Hi all,

Can any one direct me in how to use javascript confirm() function when
a condition is met from codebehind. I don't want to attach this to a
button. When I submit the page, I want to check some business rules,
and based on the response from the database, if the response is Yes, I
need to display a confirmation alert to user. When the user clicks ok,
then I should take an action, if not, exit out of the subroutine.

Below is the script I'm using in the Submit_Click() subroutine.

If Trim(Message) <> "" Then
Dim strScript As String = "<script
language=JavaScript>"

The "language" attribute is deprecated. You should use
type="text/javascript" instead.
strScript += "confirm(""" & Trim(Message) &
""");"

The "confirm" method returns a boolean. It's true if the user clicked
Yes, and false if he clicked No. You need to save this value in a variable

strScript += "var bAnswer = confirm(""" & Trim(Message) & """);"

strScript += "</script>"
If (Not
Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript",
strScript)
End If
End If

In the above code, I'm getting a message from the Sql database into
Message variable, and showing that to the user.
But I don't have a way to track what an user have selected from the
confirmation alert box.

Once the user's answer is saved in the "bAnswer" variable, you need to
transmit it to the server. There are many ways to do that.

- The easiest is probably to put the variable in a hidden form field and
then submit the form (using JavaScript). On the server, read the hidden
form field's value.

- You can use a query string, for example:

top.location = "mypage.aspx?answer=" + ( bAnswer ? "true" : "false" );

These two ways cause a callback to the server, so the page will be
refreshed.

A third way, which won't cause a callback, is to use some kind of Ajax.
In your case, a simple call using XmlHttpRequest would be sufficient. Or
else, you can use a web method.

HTH,
Laurent
 
B

bruce barker

store the answer in a hidden field:

document.getElementById('fieldid').value = confirm('ok');

where fieldid is the id of the hidden field

-- bruce (sqlwork.com)
 
S

shil

Hi everyone,

I still can't find a solution to my problem.
can any one help me?
As I explained I can't register the event to button on page load
because I have to show the confirmation to user only after checking the
database. If the user click on Yes, I want to track that value and
continue in the server side script with out resubmitting the page.

Thanks.
 
B

Brian Williams

Not sure exactly what you are asking.
What are you checking in the database?
Are you loading records and the conformation button depends on records being
present or a certain value?
Are you having problems registering the event on the button?
Are you looking for a way to call server side code from client side code
like using AJax or Ajax.Net?

Regards,
Brian K. Williams
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top