简体   繁体   中英

Div doesn't update in asp.net SignalR

I'm working with SignalR and I don't want to use JavaScript to connect to the hub.

This is the part of my codes:

index.aspx:

<form id="form1" runat="server">
    <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <div>
                <div runat="server" id="DivMessage" style="width:100%; height: 250px;"></div>
                <asp:TextBox runat="server" ID="TextBoxName"></asp:TextBox>
                <asp:Button runat="server" ID="ButtonLogin" Text="LOGIN" OnClick="ButtonLogin_Click"/>
                <asp:Button runat="server" ID="ButtonMessage" Text="SEND" OnClick="ButtonMessage_Click"/>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

index.aspx.cs:

namespace ASPNetSignalR
{
    public partial class index : System.Web.UI.Page
    {
        private static string uri = "http://localhost:4933";
        private static HubConnection connection;
        private static IHubProxy proxy;

        ...
     }

     protected void ButtonLogin_Click(object sender, EventArgs e)
     {
        RunSignalRClient();
     }

     protected void ButtonMessage_Click(object sender, EventArgs e)
     {
        SendMessage("Ehsan: Hello!");
     }

     public void RunSignalRClient()
     {
        connection = new HubConnection(uri);
        proxy = connection.CreateHubProxy("ASPNetSignalRHub");

        connection.Start().Wait();

        proxy.On<string>("SendMessage", SendMessage =>
        {
            DivMessage.InnerHtml += "<p>" + SendMessage + "</p>";
        });
    }

    public void SendMessage(string message)
    {
        proxy.Invoke<string>("SendMessage", new object[] { message });
    }
}

Startup.cs:

using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(ASPNetSignalR.Startup))]
namespace ASPNetSignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

When I run this, everything works correctly and DivMessage.InnerHtml += "<p>" + SendMessage + "</p>"; runs (like the following image), but nothing shows in the web page.

图像截屏

Where I wrong?

Thanks

尝试以下操作:

$("#DivMessage").append(SendMessage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM