Asp.Net Web 表单中的 SignalIR 和 KnockoutJS [英] SignalIR and KnockoutJS in Asp.Net Web Form

查看:15
本文介绍了Asp.Net Web 表单中的 SignalIR 和 KnockoutJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MVC 平台上看到了 SignalIR 和 KnockoutJS 示例,但在 WebForm 上没有.请建议我,我们可以在 WebForm 上使用吗?任何文章链接将是可观的.

解决方案

我知道这正好晚了一个月,但这里有一个简单的例子 [这是我通过探索MVC 示例]

假设您有一个名为 MyPage 的页面

在 .aspx 文件中写入以下内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %><!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="服务器"><title></title><script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script><script src="Scripts/jquery.signalR-0.5.2.min.js" type="text/javascript"></script><script type="text/javascript" src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script><script type="text/javascript">$(函数(){var conChat = $.connection.chat;conChat.addMessage = 函数(消息){$('#disMess').append('
  • ' + message + '
  • ');};$("#btnSend").click(function () {conChat.send($('#txtMess').val());$('#txtMess').val('');});$.connection.hub.start();});<身体><form id="form1" runat="server"><div><ul id="disMess"></ul><input id="txtMess"/><!-- 参见 onclick 也--><input id="btnSend" type="button" value="Send"/>

    </表单>

    实际上 .cs 文件 [或背后的代码] 中没有任何内容

    您需要添加 ASP.NET 文件夹Add_Code"并使用以下代码在其中放置一个类Chat.cs":

    使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 SignalR.Hubs;命名空间 NewSignalRChat{公开课聊天:中心{公共无效发送(字符串味精){Clients.addMessage(msg);}}}

    I seen may samples of SignalIR and KnockoutJS samples on MVC platform but not on WebForm. Please suggest me, can we use on WebForm? Any articles link would be appreciable.

    解决方案

    I know this is exactly a month late but here's a quick example [this is a trivial example that i have built from exploring MVC examples]

    lets say u have a page called MyPage

    in the .aspx file write the following:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>
    
    <!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 src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script src="Scripts/jquery.signalR-0.5.2.min.js" type="text/javascript"></script>
        <script type="text/javascript" src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>
    
        <script type="text/javascript">
            $(function () {
                var conChat = $.connection.chat;
                conChat.addMessage = function (message) {
                    $('#disMess').append('<li>' + message + '</li>');
                };
                $("#btnSend").click(function () {
                    conChat.send($('#txtMess').val());
                    $('#txtMess').val('');
                });
                $.connection.hub.start();
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <ul id="disMess"></ul>
            <input id="txtMess" />
            <!-- see onclick also -->
            <input id="btnSend" type="button" value="Send" />
        </div>
        </form>
    </body>
    </html>
    

    and actually nothing in the .cs file [or the code behind]

    you'll need to add the ASP.NET folder "Add_Code" and place a class "Chat.cs" in it with the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using SignalR.Hubs;
    
    namespace NewSignalRChat
    {
        public class Chat : Hub
        {
            public void Send(string msg)
            {
                Clients.addMessage(msg);
            }
        }
    }
    

    这篇关于Asp.Net Web 表单中的 SignalIR 和 KnockoutJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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