Scroll Panel to bottom (last line) after ASP.Net Ajax Timer refresh using C# and VB.Net

nauna
 
on Jun 12, 2021 01:20 AM
Sample_235523.zip
1323 Views

hello,

Scroll panel on bottom on ajax timer refresh

considering this logic Scroll Panel to bottom (last line) on Button Click using C# and VB.Net in ASP.Net

after timer runs the script does not take panel scroll to bottom

pls advice.

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ScrollPanelBottom() {
        var text = $('[id*=Panel1]');
        text.scrollTop(text[0].scrollHeight);
    };
</script>
<div class="comment">  
<asp:Panel ID="Panel1" runat="server" Height="100px" ScrollBars="Vertical">
<asp:UpdatePanel runat="server" ID="upecompetechatlist" UpdateMode="Conditional" >
    <ContentTemplate>      
        <asp:Timer ID="timer" runat="server" Interval="1000" OnTick="Timer1_Tick" >
        </asp:Timer>  
        <asp:Label ID="lbltext" runat="server" Text="Text"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />
    </Triggers> 
</asp:UpdatePanel>
</asp:Panel>                 
</div>

 

int a = 1;
protected void Timer1_Tick(object sender, EventArgs e)
{
    // your stuff to refresh after some interval
    lbltext.Text += "<br/> Message "  + a++;
    string script = "window.onload = function() { ScrollPanelBottom(); };";
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", script, true);
}

protected void Button1_Click(object sender, EventArgs e)
{
    string script = "window.onload = function() { ScrollPanelBottom(); };";
    ClientScript.RegisterStartupScript(this.GetType(), "", script, true);
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 12, 2021 05:43 AM
on Jan 24, 2022 09:09 AM

Hi nauna,

Check this example. Now please take its reference and correct your code.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ScrollPanelBottom() {
        var text = $('[id*=Panel2]');
        text.scrollTop(text[0].scrollHeight);
    };

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_endRequest(function (sender, e) {
            if (sender._postBackSettings.panelsToUpdate != null) {
                var text = $('[id*=Panel2]');
                text.scrollTop(text[0].scrollHeight);
            }
        });
    };
</script>
<asp:ScriptManager runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<div class="comment">
    <asp:Panel ID="Panel2" runat="server" Height="100px" ScrollBars="Vertical" Style="background-color: antiquewhite;">
        <asp:UpdatePanel runat="server" ID="upecompetechatlist" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Timer ID="timer" runat="server" Interval="1000" OnTick="Timer1_Tick">
                </asp:Timer>
                <asp:Label ID="lbltext" runat="server" Text="Text"></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>
    </asp:Panel>
</div>

Code

C#

static int a = 31;
protected void Timer1_Tick(object sender, EventArgs e)
{
    // your stuff to refresh after some interval
    lbltext.Text += "<br/> Message " + a;
    a++;
}

protected void Button1_Click(object sender, EventArgs e)
{
    string script = "window.onload = function() { ScrollPanelBottom(); };";
    ClientScript.RegisterStartupScript(this.GetType(), "message", script, true);
}

VB.Net

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
    lbltext.Text += "<br/> Message " & a
    a += 1
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim script As String = "window.onload = function() { ScrollPanelBottom(); };"
    ClientScript.RegisterStartupScript(Me.GetType(), "message", script, True)
End Sub

Screenshot