通过JavaScript调用C#函数(不JSON) [英] Calling C# function through Javascript (without Json)

查看:117
本文介绍了通过JavaScript调用C#函数(不JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为callfunction()在JavaScript中(Mypage.aspx)。这函数应该调用另一个函数FUNC()在C#中的函数(Mypage.aspx.cs)

I have a function named "callfunction()" in JavaScript(Mypage.aspx) .This function should call another function "func()" in C# (Mypage.aspx.cs )

事情是这样的:

(在Mypage.aspx)
     

(in Mypage.aspx)

 function callfunction()

 {

  // i have to call func() function here .....

 }

 </script>

(在Mypage.aspx.cs文件)

(in Mypage.aspx.cs file)

 public void func()

 {

 // My code goes here

 }

我有很多研究,因为这一点,我结束了,到目前为止有2个结论:
第一个是使用JSON,但我的上司明确表示,他们不希望我这样做。
第二个是,我不能这样做,因为我想是因为客户端,服务器ASPNET协议

I have researched alot because of this and i ended up so far with 2 conclusions: 1st was to use Json, but my superiors said clearly that they dont want me to do so. 2nd was that i cant do as i wish because of the client, server aspnet protocol

请给我如何做到这一点的任何一种提示的,我越来越绝望

Please give me any kind of tip in how to do this, i am getting desperate

推荐答案

如果这是一个web表单的项目(不MVC),你不希望使用AJAX,你可以使用的 __ doPostBack

If it's a webforms project (not MVC) and you don't want to use AJAX, you can use __doPostBack.

<script type="text/javascript">
 function callfunction(parameter)
 {
     __doPostBack('func', parameter)
 }
</script>

C#:

public void Page_Load(object sender, EventArgs e)
{
  string parameter = Request["__EVENTARGUMENT"]; // parameter
  var senderObject = Request["__EVENTTARGET"]; // func
  if(senderObject == "func")
  {
     //call your function here, or write the implementation
  }
}

这篇关于通过JavaScript调用C#函数(不JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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