开发者

Method doesn't exist if declared in the page

I have a page defined as:

<%@ Page Language="C#" %>
<html>

<head>
<title>Untitled 1</title>
<script type="text/c#">
    public void WriteHello()
    {
        Response.Write("HELLO EVERYBODY");
    }
</script>
</head>
<body>
    <div>
        <% WriteHello(); %>
    </div>
</body>
</html>

But this throws the compilation error of:

The name 'WriteHello' does not exist in the current context

If I move the C# code to a seperate file, and link to it, it works as expected. But for this I need to keep it in the same file. Can you not call inline开发者_高级运维 methods like this? Or am I missing something very obvious?


The script tag you have coded is a client side script - it will try to execute on the browser. The code that tries to use it runs on the server.

You need to change the script to be server side script:

<script runat="server">

You can write this as:

<%
    public void WriteHello()
    {
        Response.Write("HELLO EVERYBODY");
    }
%>

Which is the syntax you used elsewhere already.


You are missing

<script runat=server type="text/c#"> 


You need to change it to

<script type="text/c#" runat="server">

Otherwise your code will not get compiled at runtime

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜