Solved

Combining asp and javascript

Posted on 2004-08-25
17
883 Views
Last Modified: 2012-06-22
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_codebehind)
          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!

0
Comment
Question by:RichardFox
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 9

Expert Comment

by:crescendo
ID: 11897559
The "<%" means that you are running server-side code, and the xx function is client-side.
0
 
LVL 9

Assisted Solution

by:crescendo
crescendo earned 200 total points
ID: 11897574
Try something like

  <%
      if (some_var_set_in_my_codebehind)
          Response.Write "xx(some_other_var)"
  %>

This will put a call to xx at the same place in the output stream
0
 

Author Comment

by:RichardFox
ID: 11897617
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?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Assisted Solution

by:mmarinov
mmarinov earned 100 total points
ID: 11897649
Hi RichardFox,

the common way of doing this is by using

       String scriptString = "<script language=JavaScript>xx('" + some_other_var + "');";
       scriptString += "</script>";

           if(!this.IsClientScriptBlockRegistered("clientScript"))
              this.RegisterClientScriptBlock("clientScript", scriptString);


B..M
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11898167
<<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_codebehind)
          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>
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11899970
and why it will not work ?
what will output my code ? :)
B..M
0
 
LVL 2

Accepted Solution

by:
Freak4VB earned 200 total points
ID: 11901119
I will put in a very simple solution for this. I am giving you  the exact code, copy paste it and it would work...any explainations required..........shoot up ! :-)

<html>
<head>
<script language=javascript>
   function xx(a)
   {
      // whatever
   }
</script>
</head>
<body>
  .. form, contols, etc
  <%
      if (some_var_set_in_my_codebehind) %>
<script language = 'javascript'>
          xx(<%=some_other_var%>)
</script>
</body>
</html>
this is assuming that your some_other_var is a server side variable, else u knw what to do, just remove the server side tags from around this some_other_var !!!
0
 

Author Comment

by:RichardFox
ID: 11902773
crescendo, this page:

test.aspx:

<html>
  <head>
    <title>ServerClientTest</title>
    <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...
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11902931
Shouldn't it be "var" rather than "string" in

    string s = "es tevi milu!";
0
 
LVL 2

Expert Comment

by:Freak4VB
ID: 11903001
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.
0
 

Author Comment

by:RichardFox
ID: 11903050
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! #$%@#!
0
 
LVL 2

Expert Comment

by:Freak4VB
ID: 11903094
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 !!
0
 

Author Comment

by:RichardFox
ID: 11903171
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>myfunction();</script>";
      if (!this.IsClientScriptBlockRegistered("clientScript"))
                     this.RegisterClientScriptBlock("clientScript", s);
  %>
</body>
</html>

work?
0
 
LVL 2

Expert Comment

by:Freak4VB
ID: 11903282
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>myfunction();</script>";
     if (!this.IsClientScriptBlockRegistered("clientScript"))
                     this.RegisterClientScriptBlock("clientScript", s);

</Script>

Better yet if you can have it like this
<Script runat="server">

public void Page_Load()
{

 String s = "<script language=JavaScript>myfunction();</script>";
     if (!this.IsClientScriptBlockRegistered("clientScript"))
                     this.RegisterClientScriptBlock("clientScript", 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 !!!
0
 

Author Comment

by:RichardFox
ID: 11921818
ok, great but what is the difference between <%  %> and <script runat="server"></script> ?
0
 
LVL 2

Expert Comment

by:Freak4VB
ID: 11961714
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 !
0
 

Author Comment

by:RichardFox
ID: 11962289
ok, please let me know because I can't find anything on it
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

827 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