在ASP.NET中调用C#到Javascript [英] Call C# to Javascript in ASP.NET

查看:67
本文介绍了在ASP.NET中调用C#到Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,

如何在asp.net中调用c#中的public void(BlueOn)到javasript(StartBlink)?



C#

 名称空间 ArduinoTest 
{
public partial class LEDTest:System .Web.UI.Page
{

SerialPort ardo;

受保护 void Page_Load(对象 sender,EventArgs e)
{
ardo = new SerialPort();
ardo.PortName =& quot; COM4& quot ;;
ardo.BaudRate = 9600 ;
// BlueOn();
}

[WebMethod]
public void BlueOn()
{

string blue =& quot; 1& quot ;;
ardo.Open();
ardo.Write(蓝色);
ardo.Close();
}





Asp.Net中的JS

 <   asp:ScriptManager     ID   =  ScriptManager2    runat   =  server    EnablePageMethods   =  true >  <   / asp:ScriptManager  >  
< script 类型 = text / javascript >


函数StartBlink(){



}

解决方案

您可以在页面上注册客户端脚本并运行服务器端脚本。

例如如何:动态添加客户端脚本到ASP.NET网页 [ ^ ]。


如果你想从你的代码文件(.cs文件)调用客户端(JavaScript)函数,而不是像下面这样使用。





 ScriptManager.RegisterStartupScript( this  .Page, this  .GetType( ),  start < span class =code-string> StartBlink();, true ); 





以上行将调用 StartBlink()方法这是在客户端。



希望它会有所帮助。 :)


Please help me,
how to call a public void (BlueOn) in c # to javasript (StartBlink) in asp.net?

C#

namespace ArduinoTest
{
    public partial class LEDTest : System.Web.UI.Page
    {

        SerialPort ardo;

        protected void Page_Load(object sender, EventArgs e)
        {
            ardo = new SerialPort();
            ardo.PortName = &quot;COM4&quot;;
            ardo.BaudRate = 9600;
            //BlueOn();
        }

        [WebMethod]
        public void BlueOn()
        {

            string blue = &quot;1&quot;;
            ardo.Open();
            ardo.Write(blue);
            ardo.Close();
        }



JS in Asp.Net

<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true"></asp:ScriptManager>
        <script type="text/javascript">


            function StartBlink() {



            }

解决方案

You can register client side scripts and run server side scripts on the page.
For e.g. How to: Add Client Script Dynamically to ASP.NET Web Pages[^].


If you want to Call a client side(JavaScript) function from your code file(.cs file), than you can use like below.


ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "start", "StartBlink();", true);



The above line will call your StartBlink() Method which is on client side.

Hope it will help. :)


这篇关于在ASP.NET中调用C#到Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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