【问题标题】:Server code in JavascriptJavascript 中的服务器代码
【发布时间】:2010-01-18 11:51:03
【问题描述】:

我有:

Page.aspx
Page.aspx.vb
TestClass.vb

我正在尝试从 Page.aspx 访问 TestClass 类的共享属性。

这段代码运行良好:

...
<head>
<script language="JavaScript">

    <% if System.Globalization.CultureInfo.CurrentCulture.Name.ToLower = "pt-br" Then %>
        alert('portugues');
    <% else %>
        alert('ingles');
    <% end if %>

</script>
</head>
...

但是当我尝试访问 TestClass 的共享属性时,我得到一个异常:

<% if TestClass.Idioma = TestClass.TipoIdioma.Portugues Then %>
    alert('portugues');
<% else %>
    alert('ingles');
<% end if %>

错误 BC30451:名称“TestClass”未定义。

这是课程:

Public Class TestClass

    Public Enum TipoIdioma
        Portugues
        Ingles
    End Enum

    Public Shared ReadOnly Property Idioma() As TipoIdioma
        Get
            If System.Globalization.CultureInfo.CurrentCulture.Name.ToLower = "pt-br" Then
                Return TipoIdioma.Portugues
            Else
                Return TipoIdioma.Ingles
            End If
        End Get
    End Property

End Class

【问题讨论】:

  • 我想我们可能需要更多信息。您的 TestClass.vb 文件在哪里?是在 App_Code 目录下吗?

标签: javascript vb.net properties shared


【解决方案1】:

您需要创建TestClass 的新实例。试试这样的:

<script language="JavaScript">

    <%
    Dim tc = new TestClass()
    if TestClass.Idioma = TestClass.TipoIdioma.Portugues Then %>
        alert('portugues');
    <% else %>
        alert('ingles');
    <% end if %>

</script>

【讨论】:

    【解决方案2】:

    不太清楚,但您的类是否在命名空间中?您可能需要将命名空间导入到您的 aspx 文件中。

    <%@ Import Namespace="MyNamespace" %>
    

    【讨论】:

    • 它不起作用......再次出现同样的错误。 “TestClass”类不是页面的代码隐藏。我有: Page.aspx Page.aspx.vb TestClass.vb 我正在尝试从 Page.aspx 访问 TestClass 类的共享属性。
    猜你喜欢
    • 2021-07-12
    • 2011-06-28
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多