<%@ Page Language="VB" Debug="False" Explicit="True" Strict="True"
EnableSessionState="False" Buffer="True" SmartNavigation="True"%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
'Hold variable for my Record Count
Public RcdCount As Integer
Sub Page_Load(Sender As Object, E As EventArgs)
'If page is posted for the first time
If Not Page.IsPostBack Then
'Reset my Datagrid to page 1
MyDataGrid.SelectedIndex = 0
'Call my datagrid method
BindMyDataGrid()
End If
End Sub
Sub MyDataGrid_Page(Sender As Object, E As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindMyDataGrid()
End Sub
Sub BindMyDataGrid()
'Programmatic Caching Setup
Dim DataGridCache As DataSet = CType(Cache.Get("DataGridCache"),DataSet)
'Check if Cache item is equal to Nothing
If DataGridCache is Nothing Then
Const CommandText As String = "SELECT FAQID, Description FROM tblFAQ ORDER BY FAQID"
'The connection to our database
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim myCommand As New SqlDataAdapter(CommandText, myConnection)
Dim DS As New DataSet()
myCommand.Fill(DS)
MyDataGrid.DataSource = DS
'Insert dataset into cache
Cache.Insert ("DataGridCache", DS, Nothing, DateTime.Now.AddSeconds(360), TimeSpan.Zero)
lblCacheInfo.text = "DataGrid was populated from the database..."
'Specify what time the cache was updated
Application("TimeCachedDataSetAdded") = DateTime.Now
'Determine how many total records we have
RcdCount = CInt(DS.Tables(0).Rows.Count.ToString())
Else
lblCacheInfo.text = "DataGrid was used from cache. The cache was populated at " & _
Application("TimeCachedDataSetAdded").ToString()
'Populate datagrid from cache.
MyDataGrid.DataSource = DataGridCache
RcdCount = CInt(DataGridCache.Tables(0).Rows.Count.ToString())
End If
'Bind the datagrid from either source
MyDataGrid.DataBind()
ShowStats()
End Sub
Sub ShowStats()
If RcdCount > 1 Then
lblPageCount.Text = "<font size=2><b>" & RcdCount & " found <BR>Page " & _
MyDataGrid.CurrentPageIndex + 1 & " of " & MyDataGrid.PageCount & "</b></font>"
Else
lblPageCount.Text = vbNullString
End If
End Sub
</script>
<i>The current time is <%=DateTime.Now%></i>
<p>
<b>Caching Information</b><br />
<asp:Label id="lblCacheInfo" runat="server" />
<hr size="1" />
<asp:Label id="lblPageCount" runat="server" Font-Bold="True" Font-Size="XX-Small"></asp:Label>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server" Font-Bold="True"
Font-Size="Small" Font-Names="Verdana"
BorderColor="#DEBA84" AutoGenerateColumns="True"
BorderWidth="1px" BorderStyle="None"
AllowPaging="True" PageSize="10"
PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode="NumericPages"
OnPageIndexChanged="MyDataGrid_Page"
HeaderStyle-Font-Bold="True" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="Black"
HeaderStyle-BackColor="#FFF7BD"
BackColor="#DEBA84" CellPadding="3"
CellSpacing="2" width="85%" />
</form>
|