且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Javascript访问C#类

更新时间:2023-02-12 13:05:24

你无法使用JavaScript访问C#类 - 从来没有办法做到这一点。您所能做的就是使用<%...%> ,然后通过它调用您的类方法。

There's no way you can access C# class by using JavaScript - NEVER you can do it. All you can do is to use <% ... %> and then call your class method through that.

以下是一个示例:

您的类有方法LIKE this(请注意,您必须将方法声明为 public 在您的页面***问它):

Your class has method LIKE this (note that you must declare the method as public to access it on your page):

public String Hello()
{
    return "Hello!";
}

然后你想把它显示在你的ASPX页面中:

And then you want to diplay it in your ASPX page by this:

<body>
    <form id="form1" runat="server">
        <input type="button" value="Test" onclick="alert('<%= Hello() %>')" />
    </form>
</body>