RichardFox
asked on
Combining asp and javascript
I have the following aspx page:
<html>
<head>
<script language=javascript>
function xx(a)
{
// whatever
}
</script>
</head>
<body>
.. form, contols, etc
<%
if (some_var_set_in_my_codebe hind)
xx(some_other_var)
%>
</body>
</html>
But when I run it I am getting a compilation error from the xx() call:
Compiler Error Message: CS0103: The name 'xx' does not exist in the class or namespace 'ASP.addTrans_aspx' (name of my page)
Help!
<html>
<head>
<script language=javascript>
function xx(a)
{
// whatever
}
</script>
</head>
<body>
.. form, contols, etc
<%
if (some_var_set_in_my_codebe
xx(some_other_var)
%>
</body>
</html>
But when I run it I am getting a compilation error from the xx() call:
Compiler Error Message: CS0103: The name 'xx' does not exist in the class or namespace 'ASP.addTrans_aspx' (name of my page)
Help!
The "<%" means that you are running server-side code, and the xx function is client-side.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Duh, I'm an idiot. But the solution will not work, some_other_var is a server side variable. This must be a common thing, to pass server side vars to a client-side function, how is it normally done?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<<But the solution will not work>>
It should work, it will put a call to the client-side function into the out stream. You need:
<script>
<%
if (some_var_set_in_my_codebe hind)
Response.Write "xx(" + some_other_var + ")"
%>
</script>
which, if "some_other_var" has the value 'abc' will end up in the browser as
<script>
xx(99)
</script>
It should work, it will put a call to the client-side function into the out stream. You need:
<script>
<%
if (some_var_set_in_my_codebe
Response.Write "xx(" + some_other_var + ")"
%>
</script>
which, if "some_other_var" has the value 'abc' will end up in the browser as
<script>
xx(99)
</script>
and why it will not work ?
what will output my code ? :)
B..M
what will output my code ? :)
B..M
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
crescendo, this page:
test.aspx:
<html>
<head>
<title>ServerClientTest</t itle>
<script language=javascript>
myfunction(mystr)
{
alert(mystr);
}
</script>
</head>
<body>
SOME TEXT
<script language=javascript>
<%
string s = "es tevi milu!";
Response.Write("myfunction ('"+s+"'); ");
%>
</script>
</body>
</html>
doesn't work. THere are no errors, but myfunction() is never executed. I have no idea why, from the source code of the html in the browser, it looks like it should work. But it doesn't.
I am going to test mmarinov's code next...
test.aspx:
<html>
<head>
<title>ServerClientTest</t
<script language=javascript>
myfunction(mystr)
{
alert(mystr);
}
</script>
</head>
<body>
SOME TEXT
<script language=javascript>
<%
string s = "es tevi milu!";
Response.Write("myfunction
%>
</script>
</body>
</html>
doesn't work. THere are no errors, but myfunction() is never executed. I have no idea why, from the source code of the html in the browser, it looks like it should work. But it doesn't.
I am going to test mmarinov's code next...
Shouldn't it be "var" rather than "string" in
string s = "es tevi milu!";
string s = "es tevi milu!";
Hang on guys !
I just wanna comment on this last piece of code although it is apparent that no one is even looking at my posts :-)
Change the last part to this
<script language=javascript>
<%
dim s
s = "es tevi milu!"
Response.Write("myfunction ('"+s+"'); ")
%>
</script>
This should take care of everything
This is because you are decalring this "s" in server side script tags.
I just wanna comment on this last piece of code although it is apparent that no one is even looking at my posts :-)
Change the last part to this
<script language=javascript>
<%
dim s
s = "es tevi milu!"
Response.Write("myfunction
%>
</script>
This should take care of everything
This is because you are decalring this "s" in server side script tags.
ASKER
Au contraire Freak, I am reading your posts! But why should my server side code defautl to VB instead of C#? ANyway I put
<%@ Page language="c#" %>
At the top of this page, and my html source code in the browser looks like:
<html>
<head>
<title>ServerClient 0</title>
<script language=javascript>
myfunction(mystr)
{
alert(mystr);
}
</script>
</head>
<body>
SOME TEXT
<script language=JavaScript>
myfunction('es tevi milu!');
</script>
</body>
</html>
which looks like it should work but it doesn't! #$%@#!
<%@ Page language="c#" %>
At the top of this page, and my html source code in the browser looks like:
<html>
<head>
<title>ServerClient 0</title>
<script language=javascript>
myfunction(mystr)
{
alert(mystr);
}
</script>
</head>
<body>
SOME TEXT
<script language=JavaScript>
myfunction('es tevi milu!');
</script>
</body>
</html>
which looks like it should work but it doesn't! #$%@#!
hey man got it !!!
loll u wont b'live it !! change the declaration of myfunction dude !!
make it like this
<script language=javascript>
function myfunction(mystr)
{
alert(mystr);
}
</script>
Peace !!
loll u wont b'live it !! change the declaration of myfunction dude !!
make it like this
<script language=javascript>
function myfunction(mystr)
{
alert(mystr);
}
</script>
Peace !!
ASKER
omygod i'm an idiot!! yep that did it
one more question, for mmarinov why doesn't
<%@ Page language="c#" %>
<html>
<head>
<title>ServerClient 2</title>
<script language=javascript>
function myfunction()
{
alert('es tevi milu!');
}
</script>
</head>
<body>
SOME TEXT
<%
String s = "<script language=JavaScript>myfunc tion();</s cript>";
if (!this.IsClientScriptBlock Registered ("clientSc ript"))
this.RegisterClientScriptB lock("clie ntScript", s);
%>
</body>
</html>
work?
one more question, for mmarinov why doesn't
<%@ Page language="c#" %>
<html>
<head>
<title>ServerClient 2</title>
<script language=javascript>
function myfunction()
{
alert('es tevi milu!');
}
</script>
</head>
<body>
SOME TEXT
<%
String s = "<script language=JavaScript>myfunc
if (!this.IsClientScriptBlock
this.RegisterClientScriptB
%>
</body>
</html>
work?
I think I can answer that as well dude !!
Dont use these <% tags for the kind of operation u are attemting.
Instead wrap up ur code like this
<Script runat="server">
String s = "<script language=JavaScript>myfunc tion();</s cript>";
if (!this.IsClientScriptBlock Registered ("clientSc ript"))
this.RegisterClientScriptB lock("clie ntScript", s);
</Script>
Better yet if you can have it like this
<Script runat="server">
public void Page_Load()
{
String s = "<script language=JavaScript>myfunc tion();</s cript>";
if (!this.IsClientScriptBlock Registered ("clientSc ript"))
this.RegisterClientScriptB lock("clie ntScript", s);
}
</Script>
Make some adjustments for syntax errors as me is not really a C# guy :-)
Reason- will tell u in my next post if this thing wud work :-)
Let the Gods Rule !!!
Dont use these <% tags for the kind of operation u are attemting.
Instead wrap up ur code like this
<Script runat="server">
String s = "<script language=JavaScript>myfunc
if (!this.IsClientScriptBlock
this.RegisterClientScriptB
</Script>
Better yet if you can have it like this
<Script runat="server">
public void Page_Load()
{
String s = "<script language=JavaScript>myfunc
if (!this.IsClientScriptBlock
this.RegisterClientScriptB
}
</Script>
Make some adjustments for syntax errors as me is not really a C# guy :-)
Reason- will tell u in my next post if this thing wud work :-)
Let the Gods Rule !!!
ASKER
ok, great but what is the difference between <% %> and <script runat="server"></script> ?
to be honest....I thought i had a theory about it but the more i think about it the more I get confused...i will try and look it up on the net and see if I can find out something about it.
And thanks for accepting my post as your answer !
And thanks for accepting my post as your answer !
ASKER
ok, please let me know because I can't find anything on it