0
I have an ASP.NET panel that shows recent messages and I want the scroll to always remain at the end (messages are from bottom to top, as in Whatsapp).
My problem is that I can’t at all make the scroll stay at the end. Here’s what I’ve tried and it didn’t work:
JS:
function scrollMsgs() {
var pnl = document.getElementById("pnlMsgs");
pnl.scrollTop = pnl.scrollHeight;
}
HTML:
<div class="row mb-4">
<div class="col-12 rounded" id="divMsgs">
<asp:Panel ID="pnlMsgs" runat="server">
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
</asp:Panel>
</div>
</div>
How am I calling the job:
Protected Sub cmdPerguntar_Click(sender As Object, e As EventArgs) Handles cmdPerguntar.Click
...
lblMsg.Text &= "texto texto texto texto [...]\n"
Page.ClientScript.RegisterStartupScript(Page.GetType, "ScrollMsgs", "scrollMsgs();", True)
End Sub
The strange thing is that with the following gambit, it works:
function scrollMsgs() {
setTimeout(function () {
let objDiv = document.getElementById("pnlMsgs");
objDiv.scrollTop = objDiv.scrollHeight - objDiv.clientHeight;
}, 1);
}
Present your hmtl or view or Markup
– Leandro Angelo
I edited the question and put the panel part
– Natan Fernandes
The Scroll is only inside the
divMsgs
?– Leandro Angelo
Yes. I was able to solve it by putting a timeout to wait 1 millisecond to run the Javascript function and it worked, but this is gambiarra. It seems to me that VB.NET does everything at once instead of the C# that does it step by step
– Natan Fernandes
This has nothing to do with c# and Vb.net... you are confusing things and you need to understand what the Asp.net click on the server and the client looks like.
– Leandro Angelo
Edit your question presenting the real problem and how you were calling the function
– Leandro Angelo
I edited the answer
– Natan Fernandes