-1

Possible Duplicate:
Call ASP.NET Function From Javascript?

I have a C# function containing an if statement

if (condition) 
    test=true 
else 
    test= false 

How do I call that function in javascript and use the result of that test variable to do an if statement ?

The c# file I am referencing is the code behind (.aspx.cs) to an .aspx page. Is there not a way I can call the following function from this .aspx page.

public void write(bool complete)
{
    System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~file.txt"), true);

    if (complete == true)
    {
        writer.WriteLine("completed");
    }
    else
    {
        writer.WriteLine("FAILED");
    }
    writer.Flush();
    writer.Close();
    writer.Dispose();
}
2
  • Why could this not be done in Javascript anyway?
    – Mr Wilde
    Sep 14, 2012 at 9:35
  • this question has been asked 4 years ago and then a bah-zillion times after!
    – gideon
    Sep 14, 2012 at 10:24

4 Answers 4

2

Use an Ajax call on the client, for example

http://api.jquery.com/jQuery.get/

Assuming this is a web application, expose the C# call in as a WebMethod, or MVC action.

0
1

How about an Ajax call that will return the answer from your C# function (true/false)?

0

I'am not familiar with web technologies a lot, but as I know JS is client side script and you trying to execute server side method. So IMHO it is not possible to do directly.

0

For accessing the C# method in the server side you have to use a static method with the attribute [WebGet].

Then using ajax you can access that method and receive the information.

Not the answer you're looking for? Browse other questions tagged or ask your own question.