By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,632 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,632 IT Pros & Developers. It's quick & easy.

Calling Code-Behind from within <script>?

P: n/a
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-
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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?
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
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-

Nov 18 '05 #2

P: n/a
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-

"Felbrigg" <so*****@microsoft.com> wrote in message
news:u2**************@TK2MSFTNGP11.phx.gbl...
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?
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
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-


Nov 18 '05 #3

P: n/a
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-

"Felbrigg" <so*****@microsoft.com> wrote in message
news:u2**************@TK2MSFTNGP11.phx.gbl...
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?
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
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-


Nov 18 '05 #4

P: n/a
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)
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e#**************@TK2MSFTNGP10.phx.gbl...
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-

Nov 18 '05 #5

P: n/a
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.

"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e#**************@TK2MSFTNGP10.phx.gbl...
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-

Nov 18 '05 #6

P: n/a
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-
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
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.

"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e#**************@TK2MSFTNGP10.phx.gbl...
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-


Nov 18 '05 #7

P: n/a
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.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
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-

"Felbrigg" <so*****@microsoft.com> wrote in message
news:u2**************@TK2MSFTNGP11.phx.gbl...
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?
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
> 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-
>
>



Nov 18 '05 #8

P: n/a
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-

"Chris Jackson" <chrisjATmvpsDOTorgNOSPAM> wrote in message
news:uq*************@tk2msftngp13.phx.gbl...
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.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
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-

"Felbrigg" <so*****@microsoft.com> wrote in message
news:u2**************@TK2MSFTNGP11.phx.gbl...
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?
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
> 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-
>
>



Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.