The following code:
<%
Response.Write("VB comes second!")
'page1()
Dim p
Set p = New Page
%>
<script language = "Python" runat="server">
Response.Write("Python comes first!")
class Page:
def display_top(self):
Response.Write("""
<html>
""")
def display_body(self, body_text):
Response.Write("<body>"+body_text+"</body>")
def display_bottom(self):
Response.Write("""
</html>
""")
def page1():
p = Page()
p.display_top()
p.display_body("This is my body!")
p.display_bottom()
</script>
Gives the error:
Error Type:
Microsoft VBScript runtime (0x800A01FA)
Class not defined: 'Page'
/website/index.asp, line 6
But why?
If I call the function page1() from within the VBScript, then it works as expected.
Thanks,
Barry
EDIT 1:
This:
<script language = "Python" runat="server">
class Page:
def display_top(self):
Response.Write("<html>")
</script>
<%
Dim p
Set p = server.createobject("Page")
%>
Gives the error:
Invalid class string
If I use:
Set p = New Page
instead, then I get:
Class not defined: 'Page'
I can't find a definitive source for this, but I'm pretty sure that the VBScript New keyword can only be used to instantiate classes defined in VBScript using the Class keyword. What you should do instead is write a "factory" function in Python that will return a new Page object. Then you can call that function from VBScript.
I don't know Python, but here's an example with VBScript and JScript to illustrate the point:
<%
Dim p
Set p = makePage("hello")
Response.Write p.foo
%>
<script language="JScript" runat="server">
function Page(foo) {
this.foo = foo;
}
function makePage(foo) {
return new Page(foo);
}
</script>
You are hitting a asp engine limitation right there.
Code in <script runat=server> blocks may run at different times.
Server Script Order of Execution
Inline server script runs sequentially, top to bottom. You can define callable routines (functions or subroutines) in server script, and they will be called as needed.
All inline script has to be in the same language namely, the language specified in the # directive at the top of the page. Therefore, you can't mix scripting languages in inline script.
"But wait!" you might say. It is theoretically possible to put inline-like script into a <SCRIPT> element that is, have script in the element that is not part of a function or subroutine, as in the following example:
<% Response.Write("Some inline script<BR>")%>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Response.Write("Script in a SCRIPT element<BR>")
</SCRIPT>
Yes, you can do this. *However, you are then at the mercy of the order of execution of the IIS ASP processor.*
Ordering Script Blocks
When you are mixing languages, the order in which <SCRIPT> blocks appear in the page can make a difference as to whether they work properly. Consider this simple case of an inline VBScript script calling a function written in JScript:
<SCRIPT LANGUAGE="VBScript">
' Calls a JScript function
aNumber = 2
doubledNumber = doubleMe(aNumber)
document.write("The answer is " & doubledNumber)
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function doubleMe(aNumber){
return aNumber * 2;
}
</SCRIPT>
This won't work. More specifically, the document.write statement will write an empty string to the page. Why? Because at the time the VBScript block is being processed, the following JScript <SCRIPT> block has not yet been read, parsed, and made available to the page. When the browser processes the script blocks in the page, it works from top to bottom.
In this case, simply reversing the order of the script blocks solves the problem. And in fact, this type of scenario is not all that common—for the most part, <SCRIPT> blocks contain functions and subroutines that will not be called until the page has been fully loaded and all elements are available. Nonetheless, you should keep in the back of your mind the fact that pages are processed linearly and that <SCRIPT> blocks in different languages are processed separately.
To learn more, pls visit this MSDN knowledge base article.
Proof that the class defined in one script block is reachable from another script block;
<%
Response.Write "VBScript Here - 1 <p>"
hello()
apple.color = "reddish"
response.write (apple.getInfo())
%>
<script language=JavaScript runat=Server>
Response.Write("Javascript here - 2 <p>");
function hello()
{
Response.Write("Hello from JS <p>");
}
var apple = {
type: "macintosh",
color: "red",
getInfo: function () {
return this.color + ' ' + this.type + ' apple' + '<p>';
}
}
</script>
<%
Response.Write("VBScript here - 3 <p>")
%>
<script language=JavaScript runat=Server>
Response.Write("Javascript here - 4 <p>");
</script>
<%
Response.Write("VBScript here - 5 <p>")
%>
will give you this
Javascript here - 2
Javascript here - 4
VBScript Here - 1
Hello from JS
reddish macintosh apple
VBScript here - 3
VBScript here - 5
You need to reverse the script order. When your script encounters a function call, it allows that function block to be in any code block on the page within the same scope. This is not true when instantiating objects. While instantiating objects, the object code must be processed before the instantiating line. Scripting is for all intents and purposes executed linearly.
The problem is the order of execution. I don't have python installed on my server. So I did a test for you with JS vs VB - which is the same problem I referred to. Take a look;
<%
Response.Write "VBScript Here - 1 <p>"
%>
<script language=JavaScript runat=Server>
Response.Write("Javascript here - 2 <p>");
</script>
<%
Response.Write("VBScript here - 3 <p>")
%>
<script language=JavaScript runat=Server>
Response.Write("Javascript here - 4 <p>");
</script>
<%
Response.Write("VBScript here - 5 <p>")
%>
will give you this
Javascript here - 2
Javascript here - 4
VBScript Here - 1
VBScript here - 3
VBScript here - 5
Related
How can I get the current time as Universal time in classic asp. I know how to get in C# and I am getting the universal time in c# with following line ((DateTime.Now).ToUniversalTime()).ToString("s") and this code gives me time like this 2012-07-09T10:29:49
But I want to know the equivalent in classic asp. Thanks
As Ian pointed out you can generate a UTC time via Javascript.
You specified "ASP Classic" which of course includes Javascript as a language (Actually JScript, based on ECMAScript v3), so there's one answer for you: Call (new Date()).toUTCString().
If by chance you prefer to code your pages in mostly VBScript, you can mix in just a little JScript to get it done. You don't need to resort to Server.Execute or Sessions to make that happen.
This works for me:
<%# language="VBScript" %>
<script language='JScript' runat='server'>
function jsGetUTCTime() {
var d = new Date();
return d.toUTCString();
}
</script>
<script language='VBScript' runat='server'>
Function getUTCTime()
' Use JScript to get the current GMT time stamp
getUTCTime = jsGetUTCTime()
End Function
</script>
<!DOCTYPE html>
<html>
<head>
<title>Mix</title>
</head>
<body>
<h2>The time is:</h2>
<%= getUTCTime() %>
</body>
</html>
According to u229.no, there isn't any built-in way in VBScript to convert to UTC.
Take a look at the code the author provided below which uses JScript, and how you can call this from VBScript.
The GetServerGMT routine will return something like: Wed, 01 Feb 2006 15:21:59 UTC.
Function GetServerGMT()
// Use JScript to get the current GMT time stamp and store it in Session("ServerGMT")
Server.Execute "GetServerGMT.asp"
GetServerGMT = Session("ServerGMT")
End Function
And this is how the GetServerGMT.asp file with the jscript code looks like:
<%#language="jscript"%>
<%
var od = new Date();
var nd = od.toUTCString();
Session("ServerGMT") = nd;
%>
There are other jscript methods that you can use as well.
An alternative approach is
%>
<script runat="server" language="javascript">
function getTimezoneOffset() {
return (new Date()).getTimezoneOffset()
}
</script>
<%
Function nowUtc()
nowUtc=DateAdd("n",getTimezoneOffset(),Now)
End Function
Variation on above code
<%# language="VBScript" %>
<!DOCTYPE html>
<html>
<head>
<script language='Javascript' runat='server'>
// good for creating cookie timestamps that match RFC 1123
function jsGetFutureTimeGMT(minutes) {
var d = new Date();
var future = new Date();
future.setTime(d.getTime() + (minutes*60000));
var utcString = future.toUTCString();
var tz = utcString.indexOf("UTC");
if (tz > 0) {
utcString = utcString.substring(0, tz) + "GMT";
}
return utcString;
}
</script>
<title>ASP Classic RFC 1123 compliant GMT timestamp</title>
</head>
<body>
<h2>ASP Classic RFC 1123 compliant GMT timestamp</h2>
<pre>
The VBS time right now is: <%= Now() %>
The time in 10 minutes is: <%= jsGetFutureTimeGMT(10) %>
</pre>
</body>
</html>
If your application is connected to a database, you can probably get a timestamp from the database with a simple fast query. For example, in MySQL it's: SELECT unix_timestamp().
This will give you a Unix Timestamp (seconds since the Unix Epoch). From there convert into whatever format you like.
How do I register a Javascript variable on the server side (backend) and access it on the client side (Javascript file), without a hidden field, Literal, etc.?
You can use the RegisterClientScriptBlock-Function from the Page's ClientScriptManager.
Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "initMyClientVariable", "var myClientVariable=null;", True)
EDIT: according to your new informations, that you want to register a client array, use ClientScriptManager's RegisterArrayDeclaration Method.
VB.Net example:
Dim myArrayValue As String = """1"", ""2"", ""text"""
Page.ClientScript.RegisterArrayDeclaration("myClientArray", myArrayValue)
According to the new information in my comments that you need access to that variable from an external js-file: you should pass the js-array as argument to the function in the js-file. For example:
callFunctionInJsFile(checkBoxes);
You can put the following code in .aspx file ...
<script type="text/javascript" >
var date1 = "<%: DateTime.Now %>";
var date2 = "<%= DateTime.Now %>";
</script>
<%: %> works under ASP.NET 4
You can put a literal in the xml portion of the code and assign that literal some text:
myLiteral.Text = "<script language=\"javascript\">var myVar = 24;</script>";
This makes myVar globally available on the client side once it's rendered. You can also use the ClientScriptManager object to use Asp.Net to inject scripts and variables.
First place an <asp:Literal ID="Literal1" runat="server"></asp:Literal> tag in the <head> of your .aspx file. Then in the server side code in your .aspx.cs file, do something like Literal1.Text = "<script type=\"text/javascript\">var timer = 3600</script>" and you've got yout javascript variable called timer.
That's it. Have fun!
I have a page that includes javascript which I only want to run under certain conditions. To do this I have placed the scripts into an asp:placeholder
This actually seems to work but when I run a debug on the page I get the following warning.
Element 'placeholder' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.
If I move the placeholders into the body of the page the warning goes, but that means I'm left with scripts in the body which I also want to avoid. Does anyone have any hints on the best practice for this scenario?? thanks
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
dim lt as new Literal()
lt.text = "<script type='text/javascript' src='scripts/pageLoadAnimations.js'></scr" & "ipt>"
me.Header.Controls.Add(lt)
End Sub
You can include JS file straight from code behind:
If (some condition is true) Then
Page.ClientScript.RegisterClientScriptInclude("jQuery", "jquery-version.js")
End If
A couple of ways which fit your needs are:
Firstly, you could change your <head> tag to <head id="header" runat="server"> then this allows you to dynamically add anything into it, e.g.
dim lt as new Literal()
lt.text = "<script type='text/javascript' src='pathtojavascriptfile'></script>"
me.Header.Controls.Add(lt)
Or you could create a Public string on your page, then stick the javascript in this.
Public _JS as string
Page_Load
_JS = "alert('here');" ' Or what ever your javascript is
ASPX Page
<head>
<script type="text/javascript" src="jquery-version.js"></script>
<script type="text/javascript">
$().ready(function(){
<%=(me._JS) %>
});
</script>
</head>
You might consider looking into the ClientScriptManager. This will allow you to inject scripts into the header properly using whatever conditions you require.
Including Custom Client Script in ASP.NET Pages
ClientScriptManager Class
calling a js function using onclick...
onclick="SubmitAge(66, 'ctl00_MainContent_arTo_upAgeRange')"
function just calls an updatePanel from the client... but its saying object expected at the onclick= part!!!
here is the function, is there anything wrong with the code?
function SubmitAge(age, UpdatePanelID) {
$get('HiddenAge').value = age;
__doPostBack(UpdatePanelID);
}
EDIT: THE SUBMITAGE FUNCTION IS INSIDE A .JS FILE (AGERANGE.JS) AND ONLY WHEN MOVED HERE DOES IT STOP WORKING: HERE IS THE LINKING METHOD/HEADERS FROM THE ASCX USERCONTROL INWHICH IT IS ALL CONTAINED...
%# Control Language="VB" ClassName="AgeRange" %
%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxCT" %
script src="AgeRange.js" type="text/javascript" /script
(arrow tags removed here as it wont display, hint: stackoverflow!!!)
im printing it like this from the server...
Public Sub AppendToSB(ByRef sb As StringBuilder, ByVal CurNo As Byte, Optional ByVal clickedNo As Byte = 0)
Dim sConfirmClick = ""
If clickedNo = CurNo Then ' maybe dont make it clickable...
sConfirmClick = "return confirm('The number " & CurNo.ToString & " is already selected, are you sure you want to select it again?');"
End If
sb.Append("<a href=""#"" onclick=""" & sConfirmClick & "SubmitAge(" & CurNo.ToString & ", '" & upAgeRange.ClientID &
"')"" runat=""server"">" & CurNo.ToString & "</a>")
End Sub
Complete rewrite of my post after several clarifications:
The problem is that the ASPX page is referencing an ASCX user control that is located in a different folder. That ASCX control has an HTML <script> tag that is using a relative path to the JS file.
The solution is to correctly resolve the URL to the JS file by using some extra code:
<script src="<%= ResolveClientUrl("MyScriptLibrary.js") %>" type="text/javascript">
</script>
To prevent the script file from being referenced multiple times I recommend using the approaches specified in this other post:
ASP.NET dynamically insert code into head
Here's what it looks like in the user control's code:
// Register a script reference:
Page.ClientScript.RegisterClientScriptInclude(GetType(), "myLibraryScript", "~/Scripts/MyScriptLibrary.js");
here is how i decided to do it, ResolveClientURL is important, static link will not always work, also the if check will prevent the script being added several times in one page if you use the control multiple times on same page...
If Not Page.ClientScript.IsClientScriptIncludeRegistered("AgeRangeJS") Then ' no point in registering it twice!
' AND its registered in the header, where it should be, not two copies in the body :)
Page.ClientScript.RegisterClientScriptInclude("AgeRangeJS", ResolveClientUrl("AgeRange.js")) ' ResolveClientUrl("AgeRange.js")
End If
I am very new to jQuery and have got a quick question.
I wish to use my server side classes in my jQuery code, something similar to this:
$(document).ready(function() {
var temp = <%# myClass.Id %>;
})
Is this possible? if so, how?
Thank you very much
This is the later question I refined my former question to:
I'm sorry, I think I didn't explain myself too well... I've got a class name User. It's a class I built in my business logic.
I've got a web page named UserProfile, inside it I've got the following property exposing the current logged in user:
public BL.User CurrUser { get { return (BL.User)Session["currUser"]; } }I want to be able to access this User class from my aspx page using Jquery. How do I do that?
The databinding syntax
<%# MyStaticClass.MyProperty %>
will only work if you call DataBind on the container (page). What you're after is most likely the following syntax:
<%= MyStaticClass.MyProperty %>
which will also give you access to you page / control members
<%= this.MyPageProperty %>
As was already mentioned you should really assign those values to java script variables and pass those variables to you JS functions.
This will only work if your javascript is embedded in your source files (e.g. the .aspx files):
<script type="text/javascript">
var id = <%# myClass.Id %>; // store as raw value
var id_string = '<%# myClass.Id %>'; // store in a string
</script>
As others have said, if the JavaScript is in your aspx page, then using server tags will work fine.
If you have your jQuery in an external script file, then you could put this in your aspx page
<script type="text/javascript">
var myClass = $('#<%= myClass.ClientID %>');
</script>
and then use the variable in your external script file
$(function() {
myClass.click( function() { ... });
});
For other options take a look at this question and answer - How to stop ASP.NET from changing ids in order to use jQuery