在JavaScript调用C#功能 [英] Call c# function in javascript

查看:239
本文介绍了在JavaScript调用C#功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  呼叫ASP.NET功能从JavaScript?


我有一个包含一个C#函数if语句

 如果(条件)
    测试=真
其他
    测试= FALSE

我如何调用JavaScript的该函数,并使用测试变量的结果做一个if语句?

我引用的C#文件是背后(.aspx.cs)在code到.aspx页。有没有一种方法,我可以从这个.aspx页面中调用下面的函数。

 公共无效写入(布尔完成)
{
    System.IO.StreamWriter作家=新System.IO.StreamWriter(使用Server.Mappath(〜file.txt的),TRUE);    如果(完整== true)而
    {
        writer.WriteLine(完成);
    }
    其他
    {
        writer.WriteLine(失败);
    }
    writer.Flush();
    writer.Close();
    writer.Dispose();
}


解决方案

在客户机上使用Ajax调用,例如:

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

假设这是一个Web应用程序,揭露C#调用作为一个WebMethod,或MVC的行动。

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();
}

解决方案

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.

这篇关于在JavaScript调用C#功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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