Solved

Javascript to confirm if asp:radiobuttonlist.selectedvalue = x

Posted on 2008-06-17
9
1,482 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

743 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

18 Experts available now in Live!

Get 1:1 Help Now