js调用C#的办法

.Net技术 码拜 9年前 (2015-05-10) 1300次浏览 0个评论
 

前台html页面上有个按钮,点击后触发js方法,js方法调用C#,请问要怎么实现
代码如下:

<html>
<head>
	 <title></title>
<script type="text/javascript">
     function bclick() {
         document.write("<%=CsharpVoid();%>");
     } 
</script>  
</head>
<body>
      <input id="Button1" type="button" value="click" onclick="bclick();" />    
</body>
</html>

c#代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Web;


namespace go
{
    
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hello");
        }

        protected string CsharpVoid()
        {
            string strCC = "hello";
            return strCC;
        }
    }
}
4分
PageMethods.CsharpVoid(funReady,funError);
function funReady(result){
       alert(result);
    }
function funError(err){ 
        alert("Error:" + err._message ); 
    }
[System.Web.Services.WebMethod]
public static string CsharpVoid()
        {
            string strCC = "hello";
            return strCC;
        }
引用 1 楼 pig357 的回复:
PageMethods.CsharpVoid(funReady,funError);
function funReady(result){
       alert(result);
    }
function funError(err){ 
        alert("Error:" + err._message ); 
    }
[System.Web.Services.WebMethod]
public static string CsharpVoid()
        {
            string strCC = "hello";
            return strCC;
        }

[System.Web.Services.WebMethod] 要引用哪个using?
js调用C#的办法

我草 这个牛逼了..js调用winfrom .. 而且 是纯winfrom 不是OCX

4分
前台js
<script type=”text/javascript” language=”javascript”>
  function Ceshi()
  {
    var a = “<%=Getstr()%>”;
    alert(a);
  }
</script>
<input type=”button” onclick=”Ceshi();” value=”js调用后台代码” /> 
后台代码
public string Getstr()
{
  string aa = “你们好啊!”;
  return aa;
}
4分
右击网站项目
添加引用
.net中 找 System.Web.Services 确定

还是推荐使用jQuery+ajax

js调用winform的方法???
不科学啊,你可以用webservice或者wcf做成服务来调用啊
引用 4 楼 Chinajiyong 的回复:

前台js
<script type=”text/javascript” language=”javascript”>
  function Ceshi()
  {
    var a = “<%=Getstr()%>”;
    alert(a);
  }
</script>
<input type=”button” onclick=”Ceshi();” value=”js调用后台代码” /> 
后台代码
public string Getstr()
{
  string aa = “你们好啊!”;
  return aa;
}

弹出来的是这个。。。。
js调用C#的办法

4分
引用 7 楼 Hisouke_S 的回复:
Quote: 引用 4 楼 Chinajiyong 的回复:

前台js
<script type=”text/javascript” language=”javascript”>
  function Ceshi()
  {
    var a = “<%=Getstr()%>”;
    alert(a);
  }
</script>
<input type=”button” onclick=”Ceshi();” value=”js调用后台代码” /> 
后台代码
public string Getstr()
{
  string aa = “你们好啊!”;
  return aa;
}

弹出来的是这个。。。。
js调用C#的办法

参考这篇http://www.cnblogs.com/diony/archive/2011/08/26/2154604.html

引用 6 楼 Chinajiyong 的回复:

js调用winform的方法???
不科学啊,你可以用webservice或者wcf做成服务来调用啊

引用 6 楼 Chinajiyong 的回复:

js调用winform的方法???
不科学啊,你可以用webservice或者wcf做成服务来调用啊

求详解

4分
是在winform中么?可以这样试试:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
webBrowser1.Document.GetElementById("bt_serch").Click += new HtmlElementEventHandler(SearchNoteList);
}

private void SearchNoteList(object ob, HtmlElementEventArgs e)  //点击了【搜索】按钮,查找关键字
{
HtmlElement el = (HtmlElement)ob;
...
}
//winform执行js函数
public object ExecJSfuncion(string func, object[] param)
        {
            return this.webBrowser1.Document.InvokeScript(func, param);
        }

//在主界面函数前添加
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class MainForm : Form
{}

借助webbrowser来实现。
添加一个winform,在该form里面添加一个webbrowser,并添加MainForm_Load事件处理。
public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            webBrowser1.ObjectForScripting = new CommonOperation();//具体公开的对象,这里可以公开自定义对象
            
       webBrowser1.Navigate(Application.StartupPath+”\DemoPage.htm”); 
        }
    }
在项目下添加类 CommonOperation代码如下
[PermissionSet(SecurityAction.Demand, Name = “FullTrust”)]
    [ComVisible(true)]
    public class CommonOperation
    {
        public string ShowMsg(string msg)
        {
            MessageBox.Show(msg);
            return “”;
        }
    }
在项目下添加DemoPage.htm,代码如下:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
    <script type=”text/javascript” charset=”utf-8″>  
      function Run(str)  
       {             
             window.external.ShowMsg(str);  
       }  
    </script>
<head>
<title></title>
</head>
<body>

    <p>
            <input id=”Button1″ type=”button” value=”button” onclick=”Run(“”Hell,Lucy!””)” /></p>

</body>
</html>

运行即可看到效果,其他复杂的通用的逻辑大家自己琢磨设计。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明js调用C#的办法
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!