Script is not working because it inside ASP.Net AJAX UpdatePanel after UpdatePanel’s Asynchronous request or Partial PostBack is completed.
Refer the below sample code for your reference how to use ASP.Net AJAX UpdatePanel endRequest event handler to Handel such situation.
And implement in yourcode as per your code logic.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<div id="div1" style="height: 100px; width: 400px; overflow: auto; border: Solid 2px Grey">
<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID"
DataSourceID="SqlDataSource1" ShowHeader="False" GridLines="None">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True"
SortExpression="OrderID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"
ItemStyle-ForeColor="Red" ItemStyle-Width="40px" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID"
ItemStyle-Width="200px" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [OrderID], [OrderDate], [CustomerID] FROM [Orders]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<script type="text/javascript">
$(function () {
setDivscroll();
});
function setDivscroll() {
$('#div1').scrollTop($('#div1')[0].scrollHeight);
}
//On UpdatePanel Refresh
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
prm.add_endRequest(function (sender, e) {
if (sender._postBackSettings.panelsToUpdate != null) {
setDivscroll();
}
});
};
</script>
</div>
</div>
</form>
</body>
</html>
C#
protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1.DataBind();
}
VB.Net
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
GridView1.DataBind()
End Sub
Screeshot