如何从代码隐藏中的signalrhub调用masterpage的javascript [英] How do I call the javascript of masterpage from signalrhub from codebehind

查看:76
本文介绍了如何从代码隐藏中的signalrhub调用masterpage的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有母版页的Web应用程序,我需要向我的应用程序的所有连接用户显示警报。我曾经使用Owin启动类和映射信号R



有人可以建议我这个问题



< b>我尝试了什么:



然后创建了一个如下的Hub



< pre lang =c#> namespace ArtWebApp
{
[HubName( artHub)]
public class ArtWebHub:Hub
{

public void 您好( )
{
Clients.All.hello();
}


public void SayMessage()
{
this .Clients.All.showmessage();

}
}
}





然后在主页面中添加了Javascript如下所示







< script src =   Scripts / jquery-1.6.4.js>< / script> 
< script src = Scripts / jquery.signalR-2.2.1.js >< /脚本>
< script type = text / javascript>
$( function (){
debugger ;
var connection = $ .hubConnection(
var hub = connection.createHubProxy(' artHub');
hub.on(' showmessage' function (){

alert(' 嗨');
});


connection.start()。done();
// connection.start(function(){

// hub.invoke('SayMessage');
// });
});

< / script>





当从同一页面调用Hub方法时,这非常有效但是当我试图从按钮点击页面调用方法时它不起作用





 < span class =code-keyword> protected   void  Button1_Click( object  sender,EventArgs e )
{ var hubContext = GlobalHost.ConnectionManager.GetHubContext< ArtWebApp.ArtWebHub>();
hubContext.Clients.All.SayMessage();
}

解决方案

function (){
调试器;
var connection =


.hubConnection(
var hub = connection.createHubProxy(' artHub');
hub.on( ' showmessage' function (){

alert(' 嗨');
});


connection.start()。done();
// 连接。 start(function(){

// hub.invoke('SayMessage ');
// });
}) ;

< / script>





当从同一页面调用Hub方法时,这非常有效但是当我试图从按钮点击页面调用方法时它不起作用





 < span class =code-keyword> protected   void  Button1_Click( object  sender,EventArgs e )
{ var hubContext = GlobalHost.ConnectionManager.GetHubContext< ArtWebApp.ArtWebHub>();
hubContext.Clients.All.SayMessage();
}


I had a web Application with master page and I need to show a alert to all connected users of my application. I had used the Owin startup class and mapped signal R

Can somebody suggest me the issue

What I have tried:

Then created a Hub like below

namespace ArtWebApp
    {
        [HubName("artHub")]
        public class ArtWebHub : Hub
        {
          
            public void Hello()
            {
                Clients.All.hello();
            }
    
    
            public void SayMessage()
            {
                this.Clients.All.showmessage();
    
            }
        }
    }



Then in the masterpage I added the Javascript as below



<script src="Scripts/jquery-1.6.4.js"></script>
        <script src="Scripts/jquery.signalR-2.2.1.js"></script>
        <script type="text/javascript">
            $(function () {
                debugger;
                var connection = $.hubConnection("")
                var hub = connection.createHubProxy('artHub');
                hub.on('showmessage', function () {
    
                    alert('Hi');
                });
    
    
                connection.start().done();
                //connection.start(function () {
    
                //    hub.invoke('SayMessage');
                //});
            });
    
        </script>



This is working perfectly when the Hub method is invoked from the same page but when I tried to call the method from button click of a page its not working


protected void Button1_Click(object sender, EventArgs e)
        {            var hubContext = GlobalHost.ConnectionManager.GetHubContext<ArtWebApp.ArtWebHub>();
            hubContext.Clients.All.SayMessage();    
        }

解决方案

(function () { debugger; var connection =


.hubConnection("") var hub = connection.createHubProxy('artHub'); hub.on('showmessage', function () { alert('Hi'); }); connection.start().done(); //connection.start(function () { // hub.invoke('SayMessage'); //}); }); </script>



This is working perfectly when the Hub method is invoked from the same page but when I tried to call the method from button click of a page its not working


protected void Button1_Click(object sender, EventArgs e)
        {            var hubContext = GlobalHost.ConnectionManager.GetHubContext<ArtWebApp.ArtWebHub>();
            hubContext.Clients.All.SayMessage();    
        }


这篇关于如何从代码隐藏中的signalrhub调用masterpage的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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