1

I have an ASP.NET webform with a simple label. I can change the label's visibility and text on page load, or on a button press/event with no problem.

However, when I call returnMessage() on a SignalR event the method runs fully but the page does not update with the new text.

    private void returnMessage(string message)
    {
        lblError.Visible = true;
        lblError.Text = message;
    }

Here's how SignalR calls the method:

        HubProxyRegister.On<string>("UserAccountReturnMessage", (message) =>
        { returnMessage(message); });

Here's the blank page with the label:

<body>
    <form id="form1" runat="server">
    <div> 
            <asp:Label ID="lblError" runat="server" Text="Error:" Visible="False"></asp:Label>
    </div>
    </form>
</body>

Do I need to call some sort of a postback to show the updated label text since Signalr is calling returnMessage() instead of a user event?

Finn
  • 168
  • 2
  • 3
  • 15
Brian S
  • 385
  • 3
  • 16
  • Can you show the page code? – slasky Mar 02 '18 at 00:36
  • @petryuno1 Added the body, just a blank page with one label. – Brian S Mar 02 '18 at 00:39
  • 1
    Not used SignalR in this manner before, but I believe you need to use Javascript when using Web forms to refresh the data sent from the server. Check out this post: https://stackoverflow.com/questions/18143599/can-signalr-be-used-with-asp-net-webforms – slasky Mar 02 '18 at 00:44

1 Answers1

2

As petryouno1 wrote, you need some javascript on the client! That the client can execute a method with signalr on the server, signalr needs to establish a connection between server and client.

You need two parts:

Server part:

You need a hub. You can imagine this is like an endpoint. Inside the hub you define the methods which the client can call on the server. Additional to that I suggest you to use the generic type of hub. As type parameter you can set an interface in which you define the methods which can be executed from the client on the server.

Small Example:

public class YourHubName : Hub<IClient>
{
    public void MethodCanBeCalledFromClient(string message)
    {
        Clients.All.MethodCanBeCalledFromServer(message);
    }
}

public interface IClient
{
    void MethodCanBeCalledFromServer(string message);
}

Client part:

<script src="Scripts/jquery.1.7.1.min.js"></script>
<script src="Scripts/jquery.signalR-1.0.0-rc1.min.js"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">

    $(function() {

        var yourHub = $.connection.yourHubName; // <-- This establishes a connection to server.

        yourHub.client.methodCalledFromServer = function(msg) {    
        // Here you have to grap which jquery your DOM, and change it    
        };

        $.connection.hub.start();

        // After the connection is established you can also send messages 
        // from client to server (without postback) like:
        // yourHub.server.MethodCanBeCalledFromClient(/*String which you can take for example out of your DOM*/));

    });

</script>

Do not forget:

  • Wireup all signalr (read link bellow)
  • Take correct JS files

I suggest you to read: Can SignalR be used with asp.net WebForms?

For signalr basics read: https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client

Stephu
  • 3,236
  • 4
  • 21
  • 33