oteal
oteal

Reputation: 634

Call static method in static class from asp page

When I call static method from asp page I got this Compilation Error:

CS0103: The name 'Tudo' does not exist in the current context 
Line 10: <script src="<%= Tudo.getFromDefinicao("winJS") %>" type="text/javascript"></script>

Tudo is a static class which is in App_Code paste, and the namespace is the same of my asp pages.

namespace MySite
{
   public static class Tudo
   {
     public static string getFromDefinicao(string key)
     {
        //do some stuff
        return myString;
     }
   }
}

I want call getFromDefinicao(...) method from my asp, but asp don't find the class (in this case i'm calling in my MasterPage). If I call the methods inside Tudo.cs from MasterPage.cs I have no problem, and I don't need to declare "using 'namespace';" because they are in the same namespace...

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MySite.MasterPage" %>
<html>
<head>
 <script src="<%= Tudo.getFromDefinicao("winJS") %>" type="text/javascript"></script>
</head>
.......

What I have to do to solve this?

Upvotes: 1

Views: 4902

Answers (2)

oteal
oteal

Reputation: 634

Solution: add the namespace when calling the method:

<script src="<%= MyNamespace.MyStaticClass.myMethod()

If appears this error:

Compiler Error Message: CS0433: The type 'MySite.Tudo' exists in both 'C:...' and 'c:..'

Remove the class from ASP.NET Folder App_Code to another one.

Upvotes: 1

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

try to add the namespace MySite

<script src="<%= MySite.Tudo.getFromDefinicao("winJS") %>

Upvotes: 2

Related Questions