Solved

Javascript to confirm if asp:radiobuttonlist.selectedvalue = x

Posted on 2008-06-17
9
1,488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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