Calling Code-Behind from within <script>?

  • Thread starter Thread starter Brett Robichaud
  • Start date Start date
B

Brett Robichaud

I understand how code-behind can handle events for a page, but can I call a
code-behind method from within a <script> tag in my ASP.Net page, or can I
only call methods defined in other <script> sections? I can't seem to
figure out the syntax for for calling code-behind directly. The method is
within the class my page inherits from and is public, but when I try to call
it from my page I get this error:

CS1520: Class, struct, or interface method must have a return type

What am I missing here?

-Brett-
 
Does your method have a return type? Does it return anything? If it does,
does your line in the <Script> handle a retuned value. Are you using c# or
vb? You say the method is inherited and its public, is the pages class
public?
 
I've tried with return type and without. When I had return type it did in
fact return a value and the asp side assigned the return to a variable.

I am using C#. Yes the class is public and the method.

So I must be doing something stupid here. From your responses I assume that
it is legal to directly call code-behind methods from within a <script> tag
(as opposed to handling events in code-behind).

-Brett-
 
Here is a snippet of my code behind class:

public class WebForm1 : Page
{
public string Hello()
{
return "Hello";
}
}

From my asp page I have a <script> tag mixed within my html that looks like
this:

<script runat=server> string x = Hello(); </script>

With this code I get this error:

CS0120: An object reference is required for the nonstatic field, method, or
property 'Web1.WebForm1.Hello()'

This indicates I am not using the correct syntax for calling code-behind.
Any idea what I'm doing wrong? How do I get an instance of my page object
from within the script tag?

-Brett-
 
as long as the script tag has a runat=server (so its server script), it can
call code behind routines. the aspx page subclasses the code behind class,
so methods must be declared as protected or public to be in scope.

-- bruce (sqlwork.com)
 
I don't think you do understand CodeBehind, because you don't "call" it from
the Page. It IS the Page. The Page Template and the CodeBehind together
constitute a Class definition. The Page Template must inherit the CodeBehind
class (which must inherit System.Web.UI.Page, or a derivative thereof) to
create a complete Page class. In your message, you stated:

"The method is within the class my page inherits from and is public"

If your Page doesn't inherit the CodeBehind class, then it has nothing to do
with it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
My page does inherit from the codebehind class and my codebehind class
inherits from System.Web.UI.Page.

What I need to do is call a codebehind method from within a <script> section
in my page layout.

-Brett-
 
Try this in your aspx page instead:

<% String x = Hello(); %>

A code declaration block (script) is used to declare member variables and
methods in the dynamically generated code behind, which isn't necessarily
generated yet (and, hence, can't inherit from something until it is
generated). A code render block (<% %>) is the appropriate choice here. With
ASP.NET, everything is compiled and not interpreted, even if it is compiled
dynamically.
 
Wow, how unbelievably ignorant of me.

That worked perfectly. My asp inexperience is showing readily now. Thanks
a ton for clearing up such a simple problem.

-Brett-
 
Back
Top