Javascript to confirm if asp:radiobuttonlist.selectedvalue = x

I'm not great with javascript or StringBuilder. Please look at the snippet and tell me what I ahve done wrong. I get the error message 'Return statement outside function'.
thanks
Verdy
Dim st As System.Text.StringBuilder = New System.Text.StringBuilder
 
st.Append("<script language='javascript'>;")
st.Append("var decision = confirm('Do you wish to continue?');")
st.Append(" if (decision) {")
st.Append("return;")
st.Append("}")
st.Append("</script>")
 
Dim t As Type = Me.GetType
If Not ClientScript.IsClientScriptBlockRegistered(t, "Confirm_Deactivate") Then
ClientScript.RegisterClientScriptBlock(t, "Confirm_Deactivate", st.ToString)
End If

Open in new window

verdanteAsked:
Who is Participating?
 
philipjonathanConnect With a Mentor Commented:
Verdy, are you sure that the VB code (not the javascript code) is called by Page_Load()?
Thanks jcoehoorn for correcting my mistakes :)
0
 
philipjonathanCommented:
The problem lies with the javascript code.
You would need to generate the script as a javascript function.
In my example, the function name is ConfirmDeactivate, you can rename it to your choice.
st.Append("<script language='javascript'>;")
st.Append("function ConfirmDeactivate()")
st.Append("{")
st.Append("var decision = confirm('Do you wish to continue?');")
st.Append(" if (decision) {")
st.Append("return;")
st.Append("}")
st.Append("}")
st.Append("</script>")

Open in new window

0
 
apeterCommented:
If you want to execute those scripts "on load" of a page then attach that fuction to BODY tag.

....
<body onload="<your javascript function" ..
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Joel CoehoornDirector of Information TechnologyCommented:
Follow what phillipjonathan showed you for creating a function.

Additionally, use .AppendLine(), rather than append to have the result show on multiple lines.  It makes it easier to follow when debugging on the client side.  And get rid of the extra semi-colon in your script declaration:
st.Append("<script language='javascript'>;")  <-- no semi-colon in here

If you want the script to run when the page loads, do NOT put it in the onload for the body element as suggested.  Use ClientScript.RegisterStartUpScriptBlock() instead.  Otherwise you limit yourself to only one startup script, which could break things if you end up using a control that also needs a startupscript somewhere.
0
 
verdanteAuthor Commented:
Hi
I tried changing the javascript as suggested also modified it as contained in the snippet. But either way I don't get a confirm message - execution passes right over and completes the remainder of the sub.

I am using the following code to register the client script. Is that part of the problem or is something else going awry? thx Verdy
If Not ClientScript.IsClientScriptBlockRegistered(t, "Confirm_Deactivate") Then
ClientScript.RegisterClientScriptBlock(t, "Confirm_Deactivate", st.ToString)
End If

Dim st As System.Text.StringBuilder = New System.Text.StringBuilder
 
st.Append("<script language='javascript'>")
st.Append("function ConfirmDeactivate()")
st.Append("{")
st.Append("if (confirm('Do you wish to continue?')==true){")
st.Append(" return true;")
st.Append("}")
st.Append("}")
st.Append("</script>")
Dim t As Type = Me.GetType

Open in new window

0
 
Joel CoehoornDirector of Information TechnologyCommented:
Open the page in your browser, view the source from there, and see if, where, and how your script was rendered to the page.
0
 
verdanteAuthor Commented:
There is no coding for the scripted function on the page source at all. So does that mean I have to split up scripting the function from calling the function? ie javascript function is scripted in page header in script tags and then the scriptbuilder just calls the function on selection of radiobutton and consequent server side call (onselectedindexchanged)

thx

Verdy
0
 
jpompuCommented:
Hi verdante,

Here's a couple things to try:

1. More a question really, why are you using the RegisterClientScriptBlock at all? If you are not defining the javascript with values from your code behind, you don't need to register the script from the code behind. Simply put the code that you are entering through your RegisterClientScriptBlock directly into your HTML. That will elimiate any issues you may be having with this function.

2. If you do need to register the javascript from your code behind, are you using Ajax on your pages? If so, try this to register your code: ScriptManager.RegisterClientScriptBlock instead of ClientScript.RegisterClientScriptBlock.

3. How are you calling the ConfirmDeactivate() javascript function? If you are looking to display a confirm box when a button get's called for example, you can do something like this in your code behind:

button1.Attributes.Add("onclick", "return confirm('Do you wish to continue?');");

Hope one of these helps!
0
 
verdanteAuthor Commented:
Had to split up the code so the initial function was pre-specified. Then I used RegisterClientScriptBlock to call it based on radiobuttongroup selected value at runtime
thx
Verdy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.