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

Passing a variable value from Javascript to asp.net server variable

P: n/a
Hi all,

I am using javascript confirm() function to get user's confirmation
from codebehind after a condition is met. I don't want to attach the
function to the button on page load. 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 += "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 get the value of what an user have selected from the confirmation
alert box. I don't want to resubmit the form since I would loose some
of the business rules.

If I could pass the javascript variable "bAnswer" value to a server
variable, that would be great.

Any help is appreciated.
Thanks.

Jan 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
there is no way to pass the data back other than some sort of postback.
usually you put the answer into a hidden field and do a submit.

your confirm only display when the page is rerendered by the browser, so
you will need to keep the logic for the next pass in state.

render something like:

Page.RegisterStartupScript("myscript",string.Forma t(@"
<input type=hidden name=txtConfirm id=txtConfirm >
<script>
document.getElementById('txtConfirm').value = confirm('%0');
document.forms[0].submit();
</script>
",Message));
-- bruce (sqlwork.com)

shil wrote:
Hi all,

I am using javascript confirm() function to get user's confirmation
from codebehind after a condition is met. I don't want to attach the
function to the button on page load. 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 += "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 get the value of what an user have selected from the confirmation
alert box. I don't want to resubmit the form since I would loose some
of the business rules.

If I could pass the javascript variable "bAnswer" value to a server
variable, that would be great.

Any help is appreciated.
Thanks.
Jan 2 '07 #2

P: n/a
As Bruce said, you will have to have a sort of postback.

See if this helps you:

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**********************@42g2000cwt.googlegro ups.com...
Hi all,

I am using javascript confirm() function to get user's confirmation
from codebehind after a condition is met. I don't want to attach the
function to the button on page load. 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 += "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 get the value of what an user have selected from the confirmation
alert box. I don't want to resubmit the form since I would loose some
of the business rules.

If I could pass the javascript variable "bAnswer" value to a server
variable, that would be great.

Any help is appreciated.
Thanks.

Jan 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.