如何使用信号器更新页面上的数据控件

How to update a data control on a page using the signalr?

本文关键字:数据 控件 何使用 信号器 更新      更新时间:2023-09-26

我正在使用Visual Studio 2012 Ultimate,我一直在搜索使用 asp.net 信号器实时推送数据进入网络表单。我所拥有的只是关于聊天应用程序和实时移动网页中的对象。但是如果我想用数据控件更新页面,我几乎不知道如何应用他们使用的代码在它上面,就像(网格视图或列表视图)一样,通过SQLdatasource控件与我的数据库绑定。请给我一个视频教程或任何文章,介绍如何使用绑定的数据控件的信号器更新页面与数据库一起显示数据。

谢谢。。。

截至此刻,这就是我试图提出的我的班级文件:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
/// <summary>
/// Summary description for RefreshHub
/// </summary>
namespace mytestSignalR
{
    [HubName("refresh")]
    public class RefreshHub : Hub
    {
        string filename;
        [HubMethodName("update")]
        public void UpdatePage()
        {
            string conn = @"Server=(LocalDb)'v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|'Windblow.mdf";
            SqlConnection connection = new SqlConnection(conn);
            SqlDataReader dr;
            connection.Open();
            // string sql = "INSERT INTO [ProfileTab]([ID],[Name],[Address],[Age],[Occupation],[Nationality]) Values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "' ) ";
            string sql = "Select * From [ImageTab] ";
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.CommandType = CommandType.Text;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                filename = dr.GetString(0);
            }
            dr.Close();
            connection.Close();
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<RefreshHub>();
            context.Clients.All.Databind(filename);
        }

    }
}

页面上的JS代码更新:

<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery-1.6.4-vsdoc.js"></script>
    <script src="~/Scripts/jquery-1.6.4.js"></script>
    <script src="~/Scripts/jquery-1.6.4.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.0.3.js"></script>
    <script src="~/Scripts/jquery.signalR-2.0.3.min.js"></script>
    <script type="text/javascript" src="~/signalr/hubs"></script>
    <script type ="text/javascript" >
        $(function () {
            var page = $.connection.refreshHub;
            // $lview = $("#ListView1");
            page.client.databind = function (filename) {
                //  $lview = $("#ListView1");
                $("#ListView1").bind(filename);
                // $lview.bind(filename);
            };
            $.connection.hub.start().done(function () {
                page.server.updatePage();
            });
        });
    </script>

但在运行时仍然出现运行时错误。它说行: 21错误:无法获取未定义或空引用的属性"刷新中心"

它突出显示了这行代码var page = $.connection.refreshHub;我哪里做错了?我在这里错过了什么?谢谢。

首先,您似乎从未编写过客户端代码。

所以,这里有一个提示:在尝试使用SignalR之前,尝试学习大量的javascript,如果可以的话,jQuery也是如此。

你将无法像以前那样使用 SignalR 绑定服务器控件。您需要将从数据库中获取的数据转换为 JSON 字符串,然后将此信息传递给 Web 表单。

因此,您将获取此信息并将此 JSON 字符串转换为 Javascript 对象,并动态创建所需的 HTML 内容。

例如,GridView 控件只不过是一个 HTML"表"标记。

因此,您需要获取此对象,循环到其中以创建此表的所有子元素。为此使用jQuery会容易得多。

一些代码示例:

.HTML:

<button id="btnGridPopulator">Bind grid</button>
<table id="myClientGridView">
    <colgroup>
        <col width="100px" />
        <col width="200px" />
    </colgroup>
    <tr>
        <th>ID</th>
        <th>Title</th>
    </tr>
</table>

.JS:

$(function() {
    $("#btnGridPopulator").click(bindGrid);
});
function bindGrid() {
    // here I'll simulate a message, as if it came from your SignalR
    onmessage('[{"id":0,"title":"My videos"},{"id":1,"title":"My images"},{"id":2,"title":"My documents"}]');
}
function onmessage(msg)
{
    var arr = JSON.parse(msg);
    for(var i = 0; i < arr.length; i++) {
        $("#myClientGridView").append("<tr><td>" + arr[i].id + "</td><td>" + arr[i].title + "</td></tr>");
    }
}

摆弄您需要做什么的示例

更改代码,如下所示

// var page = $.connection.refreshHub;
var page = $.connection.refresh;

// page.server.updatePage();
page.server.update();

因为您更改了类名和方法名,按"中心名称"属性和"中心方法名称"属性