[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can I run Javascript inside VBScript?

Posted on 2006-03-21
16
Medium Priority
?
1,766 Views
Last Modified: 2011-06-08
I want to execute a piece of Javascript inside the button click event of a button, then execute some VB code after the Javascript.  The javascript opens a popup window, when the popup window closes, I want a record to be added to the database and display on my form.

Below is my code, followed by the error.

Sub bAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
                                   
                    Dim popupScript As String = "<script language=javascript>window.open('MsgBoxYesNo.aspx', 'CustomPopUp', 'width=200, height=200, menubar=yes, resizable=no')</script>"
                    Page.RegisterStartupScript("PopupScript", popupScript)

 ******add record to DB here*******
End Sub

<asp:Button ID="bAddPatient" runat="server"  OnClick="bAdd_Click"  Text="Add Patient(s)" Height="24px" Width="150px" />

***************error message*********************
Compiler Error Message: BC30648: String constants must end with a double quote.
I also can't switch to design view b/c  the program thinks I have no matching end tag for <script.  I think it's b/c I've embedded the javascript inside the VB script.
0
Comment
Question by:jm76
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 6

Expert Comment

by:enwhysee
ID: 16247120
You should most definitely be able to do what you're saying ... registering a javascript script via RegisterStartupScript on VB.NET

Is the VB.NET code above inside a <script> block?

Also, maybe you could split the script string to see if it makes things better. For example:
Dim popupScript As String = "<scri" + "pt language=javascript>window.open('MsgBoxYesNo.aspx', 'CustomPopUp', 'width=200, height=200, menubar=yes, resizable=no')</s" + "cript>"


Check out the following example:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIPageClassRegisterStartupScriptTopic.asp
0
 
LVL 12

Expert Comment

by:Giant2
ID: 16247130
Are you sure you closed all the " and ' ?
I believe but not sure that you must post \' instead of ' in the popupScript.
So something like:
Dim popupScript As String = "<script language=javascript>window.open(\'MsgBoxYesNo.aspx\', \'CustomPopUp\', \'width=200, height=200, menubar=yes, resizable=no\')</script>"
The error seems displaying the closing string char is not recognized or encountered too quick.

Hope this could help you.
Bye, Giant.
0
 
LVL 4

Accepted Solution

by:
abbdan earned 80 total points
ID: 16249922
View the code of the page it generates to see how its listing the JavaScript code.  If you see missing quotes simply add them to your popupscript string.
0
Independent Software Vendors: 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!

 

Author Comment

by:jm76
ID: 16250953
I changed the popupscript to



Now there are no errors but there is also no popup.  Any ideas?  Very frustrating!

Here's my code..............

Sub bAdd_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim popupScript As String = "<script language=javascript>window.open('MsgBoxYesNo.aspx', 'CustomPopUp', 'width=200, height=200, menubar=yes, resizable=no')</s" + "cript>"
Page.RegisterStartupScript("PopupScript", popupScript)

 ******add record to DB here, which works great*******
End Sub

<asp:Button ID="bAddPatient" runat="server"  OnClick="bAdd_Click"  Text="Add Patient(s)" Height="24px" Width="150px" />

End Sub
0
 
LVL 6

Expert Comment

by:enwhysee
ID: 16251222
As abbdan pointed out, when you browse to the resulting page and view source, do you see the generated script in there?

0
 
LVL 4

Expert Comment

by:abbdan
ID: 16251268
Looks like clicking the button is not refreshing the page to add the javascript code.

As I understand you will offer the client a page that has a button on it.  When the client clicks the button the page should refresh so that it adds the javascript for the button being clicked to call a CustomPopUp window?  Seems like the user clicking the button only adds the javascript to the html with nothing calling for the javascript to run at load time.  The event has already occured and therefore no action has occured to run the javascript.  
0
 

Author Comment

by:jm76
ID: 16251898
I don't really understand this question  
As abbdan pointed out, when you browse to the resulting page and view source, do you see the generated script in there?

Am I supposed to see generated script in the MsgBoxYesNO.aspx page?

Abbdan you are correct in what I'm trying to accomplish.  Can you think of an alternative way?

I also tried putting the popup window in a function and calling the function but also, nothing happens.  Example....

Dim javaScript As String = "<script language=JavaScript>GetDate('txtEndDate')</s" + "cript>"
          RegisterStartupScript("bAdd_Click", javaScript)
0
 
LVL 4

Expert Comment

by:abbdan
ID: 16257130
If you have Visual Studio.Net click on the HTML tab below the form that has the button or you can edit the form page that has the button using Notepad or your favorite editor.  The code you'll see is the html part of your aspx form and from there you can physically add the javascript code tagged to run when the button is clicked.  I think the problem is that your javascript gets added after you've already clicked the button and so it needs to be in there prior to the button being clicked.
0
 

Author Comment

by:jm76
ID: 16257410
I created a separate Javascript function to open the popupwindow and called it from the button click event.  Works great, but I still have the additional VB code that needs to run.

I tried to call the VB function from inside the javascript (e.g. VBScript:bAdd_Click) but nothing happens.  Is it possible to call VB script from a Javascript function?
0
 
LVL 4

Expert Comment

by:abbdan
ID: 16257705
VB code runs serverside so that when you click your button your client javascript runs first and then the code behind the button runs from the server in that order.  You can still place code behind the button and have the javascript in your html both working with the same button.
0
 
LVL 4

Expert Comment

by:abbdan
ID: 16257728
The event thats triggered when the button is pressed runs the VB codebehind and an additional tag in the html button control triggers the javascript call.
0
 
LVL 4

Expert Comment

by:abbdan
ID: 16502212
I'm interested.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 16506087
Me too.
jm76, where are you?
0

Featured Post

Independent Software Vendors: 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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

872 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