.Net 聊天应用程序

发布于 2024-08-21 10:30:20 字数 1389 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-08-28 10:30:20

使用 AJAX.NET 时,您可以使用 JavaScript 中的 PageRequestManager 来监视 AJAX 回调是否已完成;具体来说,它是 endRequest 事件。

据我所见,您没有向我们展示您的代码来确保滚动条始终位于底部,但从您的评论看来,它不断向下滚动 div,所以我建议您移动代码的一部分将 div 向下滚动,从您一直使用的任何 setIntervalendRequest,并在 endRequest 中包含一个检查以查看值是否已更改:

var contentLength = 0;

var requestMan = Sys.WebForms.PageRequestManager.getInstance();
requestMan.add_endRequest(function() {

    // get a reference to your ChatContent div, by whatever means you prefer
    var container = document.getElementById( /* your ChatContent id */ );

    // this is fired at the end of a javascript callback
    // check if new posts were added in this callback

    if(content.innerHTML.length > contentLength) {

        // update this var, so that the same check won't evaluate to true at next callback
        contentLength = content.innerHTML.length;

        // scroll the div to its bottom, by whichever means you were doing it before
        content.scrollTop = 100000;
    }
});

编辑

根据您的输入,我将修改我的答案,以便它使用您的代码,并且需要尽可能少的更改。你所得到的是不断向下滚动 DIV 的代码,并且你想要更改它,以便仅在 DIV 的内容发生更改时才滚动 DIV:

     <script type="text/javascript">
     var contentLength = 0; // add this global var
     Sys.Application.add_load(function() { 
         var t = document.getElementById('ChatContent'); 

         if(t.innerHTML.length > contentLength) // <- add a condition here
             t.scrollTop = t.scrollHeight; 
     });
     </script>

只需在 updatepanel 中用此替换脚本块,你应该能够让其余部分保持原样。

When working with AJAX.NET you can use the PageRequestManager in JavaScript to monitor an AJAX callback being finished; specifically, it's endRequest 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 the endRequest, and include a check in endRequest to see if the value has changed:

var contentLength = 0;

var requestMan = Sys.WebForms.PageRequestManager.getInstance();
requestMan.add_endRequest(function() {

    // get a reference to your ChatContent div, by whatever means you prefer
    var container = document.getElementById( /* your ChatContent id */ );

    // this is fired at the end of a javascript callback
    // check if new posts were added in this callback

    if(content.innerHTML.length > contentLength) {

        // update this var, so that the same check won't evaluate to true at next callback
        contentLength = content.innerHTML.length;

        // scroll the div to its bottom, by whichever means you were doing it before
        content.scrollTop = 100000;
    }
});

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:

     <script type="text/javascript">
     var contentLength = 0; // add this global var
     Sys.Application.add_load(function() { 
         var t = document.getElementById('ChatContent'); 

         if(t.innerHTML.length > contentLength) // <- add a condition here
             t.scrollTop = t.scrollHeight; 
     });
     </script>

Just replace your script block with this, in your updatepanel, and you should be able to leave the rest as it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文