public partial class Messages : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SendNotifications(); } public void SendNotifications() { string message = string.Empty; string conStr = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString; using (SqlConnection connection = new SqlConnection(conStr)) { string SqlConnection= "SELECT Message FROM [dbo].[DummyData]"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Notification = null; SqlDependency dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); message = reader[0].ToString(); } } } NotificationsHub nHub = new NotificationsHub(); nHub.NotifyAllClients(message); } private void dependency_OnChange(object sender, SqlNotificationEventArgs e) { if (e.Type == SqlNotificationType.Change) { SendNotifications(); } } } }
<script> $(function () { var notify = $.connection.notificationsHub; notify.client.displayNotification = function (msg) { $("#GridView1").html(msg); }; $.connection.hub.start(); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" /> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> <asp:BoundField DataField="Mail" HeaderText="Mail" SortExpression="Mail" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT [Message], [Id], [Mail] FROM [DummyData]"></asp:SqlDataSource> </div>
public class NotificationsHub : Hub { public void NotifyAllClients(string msg) { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>(); context.Clients.All.displayNotification(msg); } } }
That's because you replace the content of your #GridView1 element instead of appending to it...
#GridView1
You can see this chat example: http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc. It seems like it's dealing with the same issue.
li
ul
table
tr
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)