다음을 통해 공유


Caching Multiple Versions of a User Control by Using Declarative Attributes

You can cache multiple versions of a user control by simply declaring it in an .aspx file more than once. Just as with user controls that are not output-cached, you can include a user control in an ASP.NET page as many times as needed for your application. Unless you set the Shared attribute or property to true for the user control, multiple versions of the control output will be stored in the cache.

To cache multiple versions of a user control by using declarative attributes

  1. Using either the @ OutputCache directive in an .ascx file or the PartialCachingAttribute attribute in the code-behind class, specify the output cache settings for the user control.
  2. Include multiple versions of the user control in a page, including the property you defined in the class as an attribute in the element. Make sure the property values are unique on the page.

This technique can be useful if you define attributes for a user control that customizes the output of the user control depending on the value that is assigned to that attribute. The following example demonstrates this. If you use the following as the .aspx page that contains a user control, you can use the State attribute on the user control declarations to generate different versions of the user control, one for CA and one for UT.

<%@ Register TagPrefix="MyControl" TagName="StateSort" Src="fragcache.ascx" %>

<form runat="server" >
   <p>This page was generated at <%=DateTime.Now%>.
   <br>
   <MyControl:StateSort State="CA" runat="server" />
   <br>
   <MyControl:StateSort State="UT" runat="server" />
</form>

The user control, fragcache.ascx, contains the State property, which allows you to include the attribute in the user control server tags in the page.

<%@ OutputCache Duration="120" VaryByParam="None" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

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

    public String State;

    void Page_Load(Object sender, EventArgs e)  {

        String selectCmd = "select * from Authors where state = @State";

        SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
        SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);

        myCommand.SelectCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NVarChar, 2));
        myCommand.SelectCommand.Parameters["@State"].Value = State;

        DataSet ds = new DataSet();
        myCommand.Fill(ds, "Authors");

        MyDataGrid.DataSource= ds.Tables["Authors"].DefaultView;
        MyDataGrid.DataBind();

    }

</script>

<asp:datagrid id="MyDataGrid" runat="server" />
<br>
<p>This control was generated at <% =DateTime.Now %>.
[Visual Basic]
<%@ OutputCache Duration="120" VaryByParam="None" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="VB" runat="server">
    Public State As String

    Sub Page_Load(sender As Object, e As EventArgs)
   
          Dim selectCmd As String = "select * from Authors where state = @State"
   
          Dim myConnection As New SqlConnection("server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
          Dim myCommand As New SqlDataAdapter(selectCmd, myConnection)
   
          myCommand.SelectCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NVarChar, 2))
          myCommand.SelectCommand.Parameters("@State").Value = State
   
          Dim ds As New DataSet()
          myCommand.Fill(ds, "Authors")
   
          MyDataGrid.DataSource = ds.Tables("Authors").DefaultView
          MyDataGrid.DataBind()
      
    End Sub 'Page_Load
</script>

<asp:datagrid id="MyDataGrid" runat=server/>
<br>
<p>This control was generated at <% =DateTime.Now %>.

Simply including the @ OutputCache directive with a valid duration value in the .ascx file allows varied cache output of this user control.

See Also

Caching Multiple Versions of User Control Output | Caching Multiple Versions of a User Control, Based on Parameters | Web Forms User Controls | UserControl Class | PartialCachingAttribute Class