Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
}
share|improve this question
Why could this not be done in Javascript anyway? – Mr Wilde Sep 14 '12 at 9:35
this question has been asked 4 years ago and then a bah-zillion times after! – gideon Sep 14 '12 at 10:24

marked as duplicate by gideon, Alex, Clyde Lobo, PaulG, martin clayton Sep 14 '12 at 16:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

You will need to modify your method. It needs to be decorated as a WebMethod and a ScriptMethod. It also needs to be Static.

[System.Web.Services.WebMethod()] 
[System.Web.Script.Services.ScriptMethod()] 
public static void Write(bool completed)
{
    // The USING syntax is neater and takes care of closing and disposing.
    // But you can replace the contents of this method with your original
    // code if you wish.
    using(System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~file.txt"), true))
    {
        writer.WriteLine((completed) ? "completed" : "FAILED");
    }
}

You need to add a ScriptManager to your aspx page. (If it already exists, ensure it reads as follows)

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Then you can access your Page Method using Javascript

function SetWrite(completed)
{
    PageMethods.Write(completed, Callback);
}

function Callback()
{
    alert("Done");
}

// Usage
SetWrite(true);

You can learn more here

share|improve this answer

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