js调用C#的办法

Hyso 2013-06-20 05:10:10
前台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;
}
}
}
...全文
784 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqliu2008 2015-05-05
  • 打赏
  • 举报
回复
借助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> 运行即可看到效果,其他复杂的通用的逻辑大家自己琢磨设计。
pig357 2013-06-21
  • 打赏
  • 举报
回复
是在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
{}


Hyso 2013-06-20
  • 打赏
  • 举报
回复
引用 6 楼 Chinajiyong 的回复:
js调用winform的方法??? 不科学啊,你可以用webservice或者wcf做成服务来调用啊
求详解
Null_Reference 2013-06-20
  • 打赏
  • 举报
回复
引用 6 楼 Chinajiyong 的回复:
js调用winform的方法??? 不科学啊,你可以用webservice或者wcf做成服务来调用啊
EnForGrass 2013-06-20
  • 打赏
  • 举报
回复
引用 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; }
弹出来的是这个。。。。 [/quote] 参考这篇http://www.cnblogs.com/diony/archive/2011/08/26/2154604.html
Hyso 2013-06-20
  • 打赏
  • 举报
回复
引用 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;
}

弹出来的是这个。。。。
EnForGrass 2013-06-20
  • 打赏
  • 举报
回复
js调用winform的方法??? 不科学啊,你可以用webservice或者wcf做成服务来调用啊
moonwrite 2013-06-20
  • 打赏
  • 举报
回复
右击网站项目 添加引用 .net中 找 System.Web.Services 确定 还是推荐使用jQuery+ajax
EnForGrass 2013-06-20
  • 打赏
  • 举报
回复
前台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; }
by_封爱 版主 2013-06-20
  • 打赏
  • 举报
回复
我草 这个牛逼了..js调用winfrom .. 而且 是纯winfrom 不是OCX
Hyso 2013-06-20
  • 打赏
  • 举报
回复
引用 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?
pig357 2013-06-20
  • 打赏
  • 举报
回复

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

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧