-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();
}
Community
  • 1
  • 1
s j
  • 21
  • 1
  • 3

4 Answers4

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.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
1

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

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
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.

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
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.

DadViegas
  • 2,263
  • 16
  • 12