In the previous example, Using a Timer Control With an Update Panel, we just refreshed a panel from data in a list. In this more practical example, we pull the data from a live database which refreshes an UpdatePanel and an AJAX script manager. Here is a working demo.
<%@ Page Language="vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Timer Movies</title>
<style type="text/css">
.message
{
margin-left: 20px;
font-style:italic;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server" />
<asp:Timer ID="Timer1" Interval="5000" runat="server" />
<asp:UpdatePanel ID="up1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
Last Refresh <%= DateTime.Now.ToString("T") %><hr />
<asp:ListView
id="lstMovies"
DataSourceID="srcMovies"
Runat="server">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server">
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
Movie Title: <%# Eval("Title") %><div class="message">
Director: <%# Eval("Director") %></div>
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ObjectDataSource
id="srcMovies"
TypeName="Message"
SelectMethod="Select"
Runat="server" />
</form>
</body>
</html>
*** Message.vb ***
Imports System
Imports System.Collections
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Public Class Message
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Private _Director As String
Public Property Director() As String
Get
Return _Director
End Get
Set(ByVal value As String)
_Director = value
End Set
End Property
Public Shared Function [Select]() As ArrayList
Dim results As New ArrayList()
Dim conString As String = WebConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString
Dim commandText As String = "SELECT Title, Director FROM Movies ORDER BY Id DESC"
Dim con As New SqlConnection(conString)
Dim cmd As New SqlCommand(commandText, con)
Using con
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
results.Add(New Message(reader))
End While
End Using
Return results
End Function
Public Sub New(ByVal reader As SqlDataReader)
Title = CType(reader("Title"), String)
Director = CType(reader("Director"), String)
End Sub
End Class