zoukankan      html  css  js  c++  java
  • .net网站与Winform窗体的数据交互(JS调用Winform后台方法实现)

    前台页面:WebForm2.aspx

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            //获取WinForm穿过来的值
            function getWinFormValue(winFormValue) {
                document.getElementById("spWinFormValue").innerHTML = winFormValue;
            }
            //调用WinForm里的方法将值传过去
            function SetWinFormValue(WebFormValue) {

                //后台页面的方法 Call
                window.external.Call(WebFormValue);
            }

        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            WinForm传过来的值:<span id="spWinFormValue"></span>
            <br />
            请输入值:<asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
            <asp:Button ID="btn" runat="server" Text="点击我将值传入WinForm" OnClientClick="var v=document.getElementById('txtValue').value;alert(v);SetWinFormValue(v);return false;"
                Width="142px" onclick="btn_Click" />
        </div>
        </form>
    </body>
    </html>

    -------------------------------------------------------------------------------------------------------

    Form窗体后台代码:Form2.cs

    namespace WindowsFormsApp
    {

    //申名托管类型,对com是可见的
        [System.Runtime.InteropServices.ComVisible(true)]
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                this.wbWeb.Navigate(string.Format("javascript:getWinFormValue('{0}');void(0);", this.textBox1.Text));
            }

              //前台js调用的方法

            public void Call(string WebFormValue)
            {
                this.label1.Text = WebFormValue;
            }

            private void Form2_Load(object sender, EventArgs e)
            {

             //此窗体可以通过wbweb控件里面的脚本进行访问
              wbWeb.ObjectForScripting = this;
            }
        }
    }

    此方法要比前文的 使用webservice实现要简便性能要好!

  • 相关阅读:
    Ryzom简易汉化教程
    在Windows上编译运行Ryzom客户端
    在Windows(x86)上编译、配置并运行Ryzom Core(服务器/客户端)
    引擎设计与商业模式
    总结了一下新手学习Windows 8 Metro App 开发的捷径
    开始研究Ryzom Core!
    和Ryzom相关的项目简介
    关于Ryzom游戏开发的路线图
    根据 yyyymmdd格式日期取得当前日期所在周的开始和结束日期
    asp数组中REDIM的用法(动态数组)
  • 原文地址:https://www.cnblogs.com/xiaowei0705/p/2066060.html
Copyright © 2011-2022 走看看