We help IT Professionals succeed at work.

How to cache calendar lookup in master page

nonesuch
nonesuch asked
on
418 Views
Last Modified: 2010-04-21
I have a 2.0 VB .NET web site based on a master page. One element of the master page is a display of upcoming events -- it should show on every page. I have a query to pull this from the database. I don't want to query it every time someone hits every single page. And although I can easily put the code in a regular page, I'm running into issues trying to put it into the master page. What's the best way to handle this? I would like to have some mechanism where perhaps each person only hits the database to refresh calendar data once a day, and want it to be on the master page so I don't have to repeat the code elsewhere.
Comment
Watch Question

Richard LeeSoftware Enthusiast
CERTIFIED EXPERT

Commented:
Please that code within a user control and set the output caching for the usercontrol.

Author

Commented:
I'm offering 500 points here. Can you provide some examples or more information? Can I put that inside a master page section?
Software Enthusiast
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Okay, so I've put my calendar lookup and display into a user controll, and called it in my MasterPage.master file.

However, when I attempt to include the OutputCache directive, it complains:

Attribute 'OutputCache' is not a valid attribute of element 'Master'

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<%@ Register TagPrefix="Events" TagName="EventListing" src="EventListing.ascx" %>
<%@ OutputCache Duration="360" VaryByCustom="browser" VaryByParam="none" Shared="true" %> -- error on this line
...
...
<Events:EventListing id="myEvents" runat="server" />

Open in new window

Richard LeeSoftware Enthusiast
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Okay. I'm having trouble getting the control to work without throwing errors.

 

Line 2:  Imports System.Data
Line 3:  
Line 4:  Partial Class EventListing
Line 5:      Inherits System.Web.UI.Page
Line 6:  
 
Getting an error on line 4:

C:\Inetpub\wwwroot\Test\EventListing.ascx.vb(912304) : error BC30311: Value of type 'ASP.eventlisting_ascx' cannot be converted to 'System.Web.UI.UserControl'.

            CType(Me,System.Web.UI.UserControl).AppRelativeVirtualPath = "~/EventListing.ascx"
                  ~~                                                                          

here's the codefile for the control:

Imports System.Data.SqlClient
Imports System.Data
 
Partial Class EventListing
    Inherits System.Web.UI.Page
 
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
 
 
        If Not Page.IsPostBack Then
            LoadData()
        End If
 
    End Sub
 
    Public Sub LoadData()
        ' create connection
        Dim strConnString As String = System.Configuration.ConfigurationManager.AppSettings("sqlConnectionString")
        Dim objConn As New SqlConnection(strConnString)
 
        Dim selString As String
        selString = "select cdate as sdate, event from calendar_test"
 
        objConn.Open()
 
        ' create command object
        Dim sqlCommand As New SqlCommand
 
        sqlCommand.CommandText = "cal_web"
        sqlCommand.Connection = objConn
        sqlCommand.CommandType = CommandType.StoredProcedure
 
        ' create data adapter
        Dim objDA As New SqlDataAdapter()
        objDA.SelectCommand = sqlCommand
 
        ' populate dataset and close connection
        Dim objDS As New DataSet()
        objDA.Fill(objDS)
        objConn.Close()
 
        'specify the datasource and call dataBind
        dlCalendarEvents.DataSource = objDS
        dlCalendarEvents.DataBind()
 
    End Sub
 
 
End Class

Open in new window

Richard LeeSoftware Enthusiast
CERTIFIED EXPERT

Commented:
Inheritance structure for pages

User Control --> System.Web.UI.UserControl
Page --> System.Web.UI.Page
MasterPage --> System.Web.UI.MasterPage
Partial Class EventListing
      Inherits System.Web.UI.UserControl

Open in new window

Author

Commented:
Thanks for the assistance~
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.