ASP.NET - Is it possible to trigger a postback from server code?

前端 未结 5 729
温柔的废话
温柔的废话 2021-01-11 16:03

Is it possible to to programmatically trigger a postback from server code in ASP.NET? I know that it is possible to do a Response.Redirect or Server.Transfer to redirect to

5条回答
  •  情深已故
    2021-01-11 16:17

    If you are looking to initiate communication from the server, rather then polling, have a look at Microsoft's SignalR. The easiest context for this, and one the SignalR has as part of its example code is a chat application. You will be able to initiate messages from code behind and receive them as javascript events on your page.

    Server Code To Send:

    using System;
    using System.Web;
    using Microsoft.AspNet.SignalR;
    namespace SignalRChat
    {
        public class ChatHub : Hub
        {
            public void Send(string name, string message)
            {
                // Call the broadcastMessage method to update clients.
                Clients.All.broadcastMessage(name, message);
            }
        }
    }
    

    Client Code to catch server messages is the override of 'chat.client.broadcastMessage':

    
    

提交回复
热议问题