Solved

Javascript to confirm if asp:radiobuttonlist.selectedvalue = x

Posted on 2008-06-17
9
1,483 Views
Last Modified: 2013-11-06
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

0
Comment
Question by:verdante
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21810509
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
 
LVL 25

Expert Comment

by:apeter
ID: 21812301
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
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21814502
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
 

Author Comment

by:verdante
ID: 21818352
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21818391
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
 

Author Comment

by:verdante
ID: 21818474
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
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
ID: 21819156
Verdy, are you sure that the VB code (not the javascript code) is called by Page_Load()?
Thanks jcoehoorn for correcting my mistakes :)
0
 

Expert Comment

by:jpompu
ID: 21823990
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
 

Author Closing Comment

by:verdante
ID: 31468232
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now