Export (0) Print
Expand All

PartialCachingAttribute Class

Defines the metadata attribute that Web Forms user controls use to indicate how their output is cached. This class cannot be inherited.

For a list of all members of this type, see PartialCachingAttribute Members.

System.Object
   System.Attribute
      System.Web.UI.PartialCachingAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Class)>
NotInheritable Public Class PartialCachingAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class)]
public sealed class PartialCachingAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class)]
public __gc __sealed class PartialCachingAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class)
class PartialCachingAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

If a user control (.ascx file) contains an @ OutputCache directive, ASP.NET automatically generates this class when the user control is requested. You can declare this attribute in a class derived from the UserControl class to specify settings for the user control to output cache.

For more information about using attributes, see Extending Metadata Using Attributes.

Example

[Visual Basic] 
' Filename is partialcache.vb
' Create a code-behind user control that is cached
' for 20 seconds using the PartialCachingAttribute class.
' This control uses a DataGrid server control to display
' XML data.
Imports System
Imports System.IO
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

' Set the PartialCachingAttribute.Duration property to 20 seconds.
<PartialCaching(20)>  _
Public Class ctlMine
   Inherits UserControl
   Public MyDataGrid As DataGrid
   Public TimeMsg As Label
   
   
   Protected Sub Page_Load(Src As [Object], E As EventArgs)
      Dim ds As New DataSet()
      
      Dim fs As New FileStream(Server.MapPath("schemadata.xml"), FileMode.Open, FileAccess.Read)
      Dim reader As New StreamReader(fs)
      ds.ReadXml(reader)
      fs.Close()
      
      Dim [Source] As New DataView(ds.Tables(0))
      ' Use the LiteralControl constructor to create a new
      ' instance of the class.
      Dim myLiteral As New LiteralControl()
      ' Set the LiteralControl.Text property to an HTML
      ' string and the TableName value of a data source.
      myLiteral.Text = "<h6><font face=verdana>Caching an XML Table: " & [Source].Table.TableName & " </font></h6>"
      MyDataGrid.DataSource = [Source]
      MyDataGrid.DataBind()
      
      TimeMsg.Text = DateTime.Now.ToString("G")
   End Sub 'Page_Load 
End Class 'ctlMine

[C#] 
// [filename partialcache.cs]
// Create a code-behind user control that is cached
// for 20 seconds using the PartialCachingAttribute class.
// This control uses a DataGrid server control to display
// XML data.
using System;
using System.IO;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
  // Set the PartialCachingAttribute.Duration property to 20 seconds.
  [PartialCaching(20)]
  public class ctlMine : UserControl
  {
    public DataGrid MyDataGrid;
    public Label TimeMsg;

    protected void Page_Load(Object Src, EventArgs E) 
    {
        DataSet ds = new DataSet();

        FileStream fs = new FileStream(Server.MapPath("schemadata.xml"),FileMode.Open,FileAccess.Read);
        StreamReader reader = new StreamReader(fs);
        ds.ReadXml(reader);
        fs.Close();

        DataView Source = new DataView(ds.Tables[0]);
        // Use the LiteralControl constructor to create a new
        // instance of the class.
        LiteralControl myLiteral = new LiteralControl();
        // Set the LiteralControl.Text property to an HTML
        // string and the TableName value of a data source.
        myLiteral.Text = "<h6><font face=verdana>Caching an XML Table: " + Source.Table.TableName + " </font></h6>";
        MyDataGrid.DataSource = Source;
        MyDataGrid.DataBind();

        TimeMsg.Text = DateTime.Now.ToString("G");

     }
  }

[C++] 
// [filename partialcache.cpp]
// Create a code-behind user control that is cached
// for 20 seconds using the PartialCachingAttribute class.
// This control uses a DataGrid server control to display
// XML data.
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
#using <System.Web.dll>
#using <System.Data.dll>
using namespace System;
using namespace System::IO;
using namespace System::Data;
using namespace System::Web;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;

// Set the PartialCachingAttribute.Duration property to 20 seconds.
[PartialCaching(20)]
public __gc class ctlMine : public UserControl
{
public:
   DataGrid* MyDataGrid;
   Label* TimeMsg;

protected:
   void Page_Load(Object* /*Src*/, EventArgs* /*E*/) 
   {
      DataSet* ds = new DataSet();

      FileStream* fs = new FileStream(Server->MapPath(S"schemadata.xml"),FileMode::Open,FileAccess::Read);
      StreamReader* reader = new StreamReader(fs);
      ds->ReadXml(reader);
      fs->Close();

      DataView* Source = new DataView(ds->Tables->Item[0]);
      // Use the LiteralControl constructor to create a new
      // instance of the class.
      LiteralControl* myLiteral = new LiteralControl();
      // Set the LiteralControl.Text property to an HTML
      // string and the TableName value of a data source.
      myLiteral->Text = String::Format( S"<h6><font face=verdana>Caching an XML Table: {0} </font></h6>", Source->Table->TableName );
      MyDataGrid->DataSource = Source;
      MyDataGrid->DataBind();

      TimeMsg->Text = DateTime::Now.ToString(S"G");

   }
};

[Visual Basic] 
' The mark-up .ascx file that displays the output of
' the partialcache.vb user control code-behind file.
<%@ Control language="vb" inherits="ctlMine" src="partialcache.vb" %>

  <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="900"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  />

  <p>

  <i>Control last generated on:</i> <asp:label id="TimeMsg" runat="server" />

[C#] 
// The mark-up .ascx file that displays the output of
// the partialcache.cs user control code-behind file.
<%@ Control language=C# inherits="ctlMine" src="partialcache.cs" %>

  <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="900"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  />

  <p>

  <i>Control last generated on:</i> <asp:label id="TimeMsg" runat="server" />

[Visual Basic] 
' The WebForms page that contains the user control generated
' by partialcache.vb.
<%@ Register TagPrefix="Acme" TagName="Cache" Src="partialcache.vb.ascx" %>

<html>
<script language="vb" runat="server">

   Sub Page_Load(Src As [Object], E As EventArgs) 
      TimeMsg.Text = DateTime.Now.ToString("G")
   End Sub 'Page_Load

  </script>

<body>
  
  <form runat=server>
    <Acme:Cache runat=server/>
    <br>

    <i>Page last generated on:</i> <asp:label id="TimeMsg" runat="server" />

  </form>
</body>
</html>

[C#] 
// The WebForms page that contains the user control generated
// by partialcache.cs.
<%@ Register TagPrefix="Acme" TagName="Cache" Src="partialcache.cs.ascx" %>

<html>
<script language="C#" runat="server">

      void Page_Load(Object Src, EventArgs E ) {

          TimeMsg.Text = DateTime.Now.ToString("G");
      }

  </script>

<body>
  
  <form runat=server>
    <Acme:Cache runat=server/>
    <br>

    <i>Page last generated on:</i> <asp:label id="TimeMsg" runat="server" />

  </form>
</body>
</html>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

PartialCachingAttribute Members | System.Web.UI Namespace | UserControl | Web Forms User Controls | Caching Portions of an ASP.NET Page | @ OutputCache

Show:
© 2014 Microsoft