need to return value from jscript to vb.net (am using response.write)

itlotl

Member
Joined
Nov 21, 2005
Messages
8
Programming Experience
3-5
After much searching I have finally figured out a way to call a jscript "confirm" box from serverside vb.net code. The code I am using is as follows:
Dim sb As New StringBuilder("")
sb.Append("<script language='javascript'>")
sb.Append("if (confirm('Are you sure?')) {document.submit();}")
sb.Append("</script>")
Response.Write(sb.ToString())
Unfortunately, now I can't figure out how to pass the value returned by the "confirm" function back to my vb.net code.
Anybody have an idea for me?
JT
 
First add a hidden field to your html document, for example:
VB.NET:
<input type="hidden" name="myHiddenField" id="myHiddenField"/>

Then change your script to this:
VB.NET:
Dim sb As New StringBuilder("")
sb.Append("<script language='javascript'>")
sb.Append("var bConfirm = confirm('Are you sure?');")
sb.Append("var hdnField = document.getElementById('myHiddenField');")
sb.Append("if(hdnField){hdnField=bConfirm;}")
sb.Append("if (bConfirm) {document.submit();}")
sb.Append("</script>")
Response.Write(sb.ToString())

This will write the value of the confirm into the hidden field which will then be available to your server-side code after postback... for example in your vb.net code you could do something like this:
VB.NET:
dim bConfirm as Boolean
if Not IsNothing(Request.Form("myHiddenField")) then
bConfirm = cbool(Request.Form("myHiddenField"))
if bConfirm then
'do something
end if
end if

There are other ways you can accomplish this, but remember that the server never opens a dialog on the client... you have to put client-side javascript that runs in the client, then if you want to read the results from a client-side action, you have to store those somewhere that the server will be able to access the next time the page is sent back to the server, either in a cookie, in a page element or in the URL.
 
m_ogden,

I kind of took what you said and tried to adapt it to what I'm currently trying to do. However, I'm still getting an error message.

I've included the pertinent code. Can you tell me what I'm doing wrong?

In my ASP code I've assigned the following:

HTML:
BeforeClientContextClick="ContextMenuClick"
and

HTML:
<input id="contextclickvalue" type="hidden" name="contextclickvalue">

And the ContextMenuClick function is as follows:

VB.NET:
                function ContextMenuClick(node, itemText)
                {   
                    var ctxtclkval = document.getElementById('contextclickvalue');
                    if (itemText == "Add")
                        {
                           if(ctxtclkval) {ctxtclkval = confirm("Are you sure you want to add " + node.Text + "?");}
                        }
                    else if (itemText == "Edit")
                        {
                           if(ctxtclkval) {ctxtclkval = confirm("Are you sure you want to edit " + node.Text + "?");}
                        }
                    else if (itemText == "Delete")
                        {
                           if(ctxtclkval) {ctxtclkval = confirm("Are you sure you want to delete " + node.Text + "?");}
                        }
                    else if (itemText == "Move")
                        {
                           if(ctxtclkval) {ctxtclkval = confirm("Are you sure you want to move " + node.Text + "?");}
                        }
                    else
                        {
                           if(ctxtclkval) {ctxtclkval = false}
                        }
                }

Then in my vb.net code I attempted to access the variable as follows:

VB.NET:
        Dim contextConfirm As Boolean
        If Not IsNothing(Request.Form("contextclickvalue")) Then 
contextConfirm = CBool(Request.Form("contextclickvalue"))

However, the "If Not IsNothing..." line of my code is getting the following error message:

"System.FormatException: Input string was not in a correct format."

Any clue?

Thanks for the help,

JT
 
That exception is a casting error... in other words when you try to cast a string to a number, but the string is "A"... that would throw a FormatException error... so I'm surprised the error occurs on the Not IsNothing line... I would expect it on the CBool() line.

In any case, everything looks right to me except for one thing I would reco to change: every line where you have ctxtclkval = confirm(... change that to ctxtclkval.value = confirm(...

I assume your vb.net code is code-behind for your web page. If it is then your Request object and Request.Form object should be valid. I guess reply if adding the .value doesn't fix the problem.
 
re:need to return....

hey,why are you using such dangerous functions. do one thing, in your page load event, just add the following line of code (suppossing the button with which this will be linked is button1)---


button1.attributes.add("onclick","<script language=javascript>return confirm('are you sure?');</script>")
 
Pangolin,
Thanks for the suggestion. Actually I had tried to use something like that, but I'm not running it off of a button, so I couldn't get it to work. I did finally figure out how to accomplish what I needed a different way. Thanks for trying to help.

JT
 
Back
Top