0

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.

How I can do that?

Gui Imamura
  • 556
  • 9
  • 26
Or K
  • 335
  • 1
  • 5
  • 12
  • 1
    What framework are you using? ASP.NET Webforms? ASP.NET MVC? – George Stocker Apr 03 '14 at 00:32
  • possible duplicate of [Call ASP.NET Function From Javascript?](http://stackoverflow.com/questions/3713/call-asp-net-function-from-javascript) – kiprainey Apr 03 '14 at 00:41
  • @GeorgeStocker the OP said "In aspx page I have iframe." - ASPX :D – Jon P Apr 03 '14 at 00:48
  • @nathan742 MVC has aspx pages too if you're using the WebformsViewEngine (not to be confused with ASP.NET Webforms). – George Stocker Apr 03 '14 at 00:49
  • @GeorgeStocker I think what is better is: Razor or ASP.NET Web? – Jon P Apr 03 '14 at 00:51
  • @nathan742 No. Whether it's Razor or WEbformsViewEngine (MVC), the solution will be the same -- create an Ajax callable ActionResult in the Controller. If it's ASP.NET Webforms, the answer will be to use a WebMethod or create something that reacts on PostBack. WebformsViewEngine in ASP.NET MVC != Webforms. – George Stocker Apr 03 '14 at 00:52

4 Answers4

1

Try AJAX,in your aspx.cs file,add this

 [WebMethod]
public static string CallCSharpCode(){
    new book().write();
}

use AJAX call the method,

 $.ajax({
    type : "POST",
    contentType : "application/json; charset=utf-8",
    url : "your aspx page.aspx/CallCSharpCode",
    dataType : "json",
    success : function (data) {
        var obj = data.d;
        //your code
    },
    error : function (result) {
        //your code
    }
 });
Will Wang
  • 537
  • 5
  • 7
0

You will have to make postback using javascript to the server with some parameters and then the server side can access the required class and function as per the parameter.

nth
  • 141
  • 1
  • 1
  • 9
0

c# = server side, JavaScript = client side. You cannot directly interact with one from the other.

The best you can do is to have some sort of postback, (through a button click, or other method) which would then call the method in your book class.

To execute JavaScript code from c#, you need to write a JavaScript call in your rendered page, from a post back.

AaronS
  • 7,649
  • 5
  • 30
  • 56
0

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

Here's an example:

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

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

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>
MoonBoots89
  • 355
  • 2
  • 16
Jon P
  • 826
  • 1
  • 7
  • 20
  • 1
    This isn't doing what you think it is. The call to Hello() is made as the page is rendered by the server, and isn't called by the JavaScript onload event. To show this, take a look at the source for your rendered page, and you will see that your onload function just looks like: alert('Hello!');, and doesn't contain the inline server calls. – AaronS Apr 03 '14 at 13:33
  • I have said in my answer as 'like this'. Anyway, I have updated my answer. – Jon P Apr 04 '14 at 02:18
  • 1
    That's not the issue though. Your call to <%=Hello() %> is made as the page is rendered on the server, and not from any interaction on the client. Again, look at the rendered HTML before you click the button. It will actually look like: onclick="alert('Hello!')", and does not interact with the c# class at all. Try putting a break point in your c# class, and then see when you actually hit it. – AaronS Apr 04 '14 at 15:16
  • Besides, the OP does not indicate when to call the class method he needs, basically, one would just need a return from a C# method in this case (i.e. JSON), so I made a suggestion like this, note SUGGESTION. Additionally, I have said in the beggining of my answer similar to your answer – Jon P Apr 07 '14 at 01:39