如何从Javascript访问C#类 [英] How to access C# class from Javascript

查看:102
本文介绍了如何从Javascript访问C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个名为 book 的类。在ASPX页面(可以访问 book 类)中,我有一个 iframe 元素。我想在 iframe 的页面中使用Javascript来调用 book.write(),但我是不确定我是否可以使用Javascript从 iframe 内的页面调用C#方法。

I have a class named book in C#. In an ASPX page (which has access to book class), I have an iframe element. I want to use Javascript from the page in the iframe, to call book.write(), but I'm not sure if I can call a C# method from a page inside an iframe using Javascript.

我该怎么做那个?

推荐答案

你无法使用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>

这篇关于如何从Javascript访问C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆