.Net 聊天应用程序
我正在使用 vb.net 和 asp.net 开发聊天应用程序,其中我有一个 div 标签,其中我显示在线用户发布的消息,当消息更多并超过 div 标签的高度时,我的问题是什么我不舒服地查看以前的消息,因为我已将滚动条位置设置为始终指向底部,
为了清楚起见,我已粘贴我的代码,请浏览它并提出您的想法
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="ChatContent" runat="server" style="height: 250px; vertical-align: top; overflow:auto; text-align: left; margin-left: 10px; width: 97%;">
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel3" runat="server"UpdateMode="Conditional">
<ContentTemplate>
<textarea id="UserInputTextBox" cols="72" rows="3" runat="server" onkeyup="if(event.keyCode == 13) Button2.click();" >
</textarea>
<asp:Button ID="Button2" runat="server" CssClass="button" Text="Send" OnClick="Button2_Click" UseSubmitBehavior="False" />
<script type="text/javascript">
Sys.Application.add_load(function() {
var t = document.getElementById('ChatContent');
t.scrollTop = t.scrollHeight;
});
</script>
</ContentTemplate>
</asp:UpdatePanel>
谢谢
Shakthi
i am developing chat application using vb.net and asp.net where i am having a div tag inside which i am displaying the messages posted by the online users what my problem is when the messages are more and exceeds the height of the div tag then i am not comfortable to view the previous messages because i have set the scroll bar position to always to be pointed to bottom,
for clarity i have pasted my code please go through it and suggest your ideas
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="ChatContent" runat="server" style="height: 250px; vertical-align: top; overflow:auto; text-align: left; margin-left: 10px; width: 97%;">
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel3" runat="server"UpdateMode="Conditional">
<ContentTemplate>
<textarea id="UserInputTextBox" cols="72" rows="3" runat="server" onkeyup="if(event.keyCode == 13) Button2.click();" >
</textarea>
<asp:Button ID="Button2" runat="server" CssClass="button" Text="Send" OnClick="Button2_Click" UseSubmitBehavior="False" />
<script type="text/javascript">
Sys.Application.add_load(function() {
var t = document.getElementById('ChatContent');
t.scrollTop = t.scrollHeight;
});
</script>
</ContentTemplate>
</asp:UpdatePanel>
Thanks
Shakthi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 AJAX.NET 时,您可以使用 JavaScript 中的
PageRequestManager
来监视 AJAX 回调是否已完成;具体来说,它是endRequest
事件。据我所见,您没有向我们展示您的代码来确保滚动条始终位于底部,但从您的评论看来,它不断向下滚动 div,所以我建议您移动代码的一部分将 div 向下滚动,从您一直使用的任何
setInterval
到endRequest
,并在endRequest
中包含一个检查以查看值是否已更改:编辑
根据您的输入,我将修改我的答案,以便它使用您的代码,并且需要尽可能少的更改。你所得到的是不断向下滚动 DIV 的代码,并且你想要更改它,以便仅在 DIV 的内容发生更改时才滚动 DIV:
只需在 updatepanel 中用此替换脚本块,你应该能够让其余部分保持原样。
When working with AJAX.NET you can use the
PageRequestManager
in JavaScript to monitor an AJAX callback being finished; specifically, it'sendRequest
event.From what I can see, you haven't shown us your code for making sure the scrollbar is always at the bottom, but it seems from your comment that it constantly scrolls the div down, so I suggest you move the part of your code that scrolls the div down, from whatever
setInterval
you've been using, to theendRequest
, and include a check inendRequest
to see if the value has changed:EDIT
Based on your input, I'll revise my answer, so that it uses your code, and requires as few changes as possible. What you've got there, is code that keeps scrolling down the DIV, and you want to change it so that it only scrolls the DIV if the content of the DIV has changed:
Just replace your script block with this, in your updatepanel, and you should be able to leave the rest as it is.