Html控制項與Silverlight與Javascript 相互連信

來源:互聯網
上載者:User

首先,我要們在頁面添加一個html的控制項:

<input id="btn_exchange" type="button" value="exchange" />

在Silverlight的後台代碼添加

HtmlDocument doc = HtmlPage.Document;//擷取頁面的文檔
doc.GetElementById("btn_exchange").AttachEvent("click", new EventHandler(CallJavascriptMethod));//擷取頁面某一ID(btn_exchange)元素,並設定其"click"的冒泡事件。該事件會觸發哪個事件(CallJavascriptMethod)

        private void CallJavascriptMethod(object sender, EventArgs e)
        {//事件的函數
            string strMs = DateTime.Now.ToString();//商務邏輯
            HtmlPage.Window.Invoke("globalJSMethod", strMs);//非同步呼叫頁面的JS函數(globalJSMethod)並傳遞參數(strMs)
        }

頁面JS的函數:

    <script language="javascript" type="text/javascript">
        function globalJSMethod(msg) {
            alert(msg);//接收非同步呼叫的參數
        }
    </script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.