By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Javascript Confirm()

P: n/a
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.

Dec 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

shil wrote:
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 20 '06 #2

P: n/a
This link may help you to setup message and action communication between
server and client:
http://www.usableasp.net/DeveloperPa...rAndClient.htm

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"shil" <jo******@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
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.

Dec 20 '06 #3

P: n/a
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)

shil wrote:
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.
Dec 20 '06 #4

P: n/a
No doubt you will have to store these in a hidden field if you don't want to
use a lot of Ajax here. As you said you don't want to attach this to a
button. So here's an example of handling a return value such as in
confirm(message), and using those values in server side code.
http://www.codeproject.com/aspnet/Cl...nteraction.asp

"shil" <jo******@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
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.
Dec 20 '06 #5

P: n/a
http://www.aspkey.net/aspkey/_blog/i...th=8&year=2006

"shil" <jo******@gmail.comwrote in message news:11**********************@48g2000cwx.googlegro ups.com...
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.

Dec 22 '06 #6

P: n/a
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.

Jan 2 '07 #7

P: n/a
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
"shil" <jo******@gmail.comwrote in message
news:11**********************@i12g2000cwa.googlegr oups.com...
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.

Jan 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.