<form id ="form1" runat="server">
<asp:ScriptManager EnablePartialRendering="true" ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="message" runat="server"></asp:TextBox>
<asp:Button ID="sendmessage" runat="server" Text="send" OnClick="sendmessage_Click" />
<input type="hidden" id="displayname" value="<%= buyer %>" />
</ContentTemplate>
</asp:UpdatePanel>
<!-- dISPLAY messages -->
<ul id="discussion">
<% if (chatList != null) { %>
<% foreach (var item in chatList) { %>
<li><strong><%=item.buyer %> : </strong></li> <%= item.msg %>
<% } %>
<% } else { %>
<li><strong>No chat</strong></li>
<% } %>
</ul>
<%--<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" ForeColor="red" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>--%>
</div>
</form>
<!-- javascript -->
$(function () {
....
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (<%= buyer %>, message) {
// Html encode display name and message.
console.log("appended");
var encodedName = $('<div />').text(<%= buyer %>).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
.....
// start connection
$.connection.hub.start().done(function () {
$('#<%=sendmessage.ClientID %>').click(function() {
// Call the Send method on the hub.
console.log("CLicked");
chat.server.send($('#displayname').val(), $('#<%=message.ClientID%>').val());
console.log($('#<%=message.ClientID%>').val());
// Clear text box and reset focus for next comment.
$('#<%=message.ClientID%>').val('').focus();
});
});
});
<!-- Server side to push messages into SQL database -->
protected void sendmessage_Click(object sender, EventArgs e)
{
itemid = Request.QueryString["itemid"];
buyer = Request.QueryString["buyer"];
seller = Request.QueryString["seller"];
item = Request.QueryString["item"];
time = DateTime.Now.ToString("HH:mm:ss");
date = DateTime.Today.ToString("dd-MM-yyyy");
msg = message.Text;
productDAO productdao = new productDAO();
productdao.push(buyer, msg, Convert.ToInt32(itemid), seller, time, date, item);
}
第一次单击发送按钮时,消息将显示在ul标签中,该ul标签用于显示消息。但是,从第二次开始,情况恰恰相反。消息被推送到数据库中,但是消息未显示在ul标记中。我已经尝试过在线使用AJAX和其他解决方案,但似乎没有任何效果。我可以知道我的代码出了什么问题吗?