Export (0) Print
Expand All

How to: Cache Multiple Versions of a User Control Based on Parameters 

You can vary caching of user-control output in two ways:

  • Specify the user control name along with either a query string or a form POST parameter. You can do this declaratively using the VaryByParam attribute of the @ OutputCache directive. Alternatively, you can do it programmatically by setting VaryByParams property in the PartialCachingAttribute in the user control's code.

  • Specify the ID property of an ASP.NET server control contained in the user control. You can do this declaratively using the VaryByControl attribute. Alternatively, you can do it programmatically by setting the VaryByControls property in the PartialCachingAttribute.

    NoteNote

    Varying user control output to the cache based on query string or form POST parameters will work only if the user control posts back and processes the postback itself. If the user control posts back to the containing page, this type of user control output caching will not function properly.

To cache multiple versions of a user control declaratively using the VaryByControl attribute

  1. Create a user control that posts back.

  2. In the user control, include an @ OutputCache directive with Duration and VaryByControl attributes.

    NoteNote

    If you use the VaryByControl attribute in the directive, you do not need to also include the VaryByParam attribute, although you can include it and set it to "None".

  3. Set the VaryByControl attribute to the ID of a control that you want to vary the user control output by.

    For example, the following @ OutputCache directive sets the user control's expiration to 60 seconds and varies the control's output by an ASP.NET server control with ID of State:

    <%@ OutputCache Duration="60" VaryByControl="State" %>
    

To cache multiple versions of a user control programmatically using the VaryByControls property

  1. In code, create a user control code that posts back to itself.

  2. Include a PartialCachingAttribute at the beginning of the user control code.

  3. Include a value for the Duration parameter and set the VaryByControls parameter to the ID of the ASP.NET server control in the user control that you want to vary the user control output by.

    The following code example sets Duration to 60 seconds and VaryByControls to State. This code should be included before the code that extends the UserControl class.

    [PartialCaching(60, null, State, null)]
    

    <PartialCaching(60, null, State, null)>
    

To cache multiple versions of a user control declaratively using the VaryByParam attribute

  1. Create a user control that posts back to itself.

  2. In the user control, include an @ OutputCache directive with Duration and VaryByParam attributes.

    NoteNote

    If you include the VaryByControl attribute in the @ OutputCache directive for a user control, you do not need to also include the VaryByParam attribute.

  3. Set the VaryByParam attribute to the GET query string or form POST parameter that you want to vary the user control by.

    For example, the following @ OutputCache directive sets expirations for the user control to 60 seconds and varies the control's output by a the value of a form POST or query string parameter named State.

    <%@ OutputCache Duration="60" VaryByParam="State" %>
    

To cache multiple versions of a user control programmatically using the VaryByParams property

  1. In code, create a user control code that posts back to itself.

  2. Include a PartialCachingAttribute at the beginning of the user control code.

  3. Include a value for the Duration parameter and set the VaryByParams parameter to the GET query string or form POST parameter that you want to vary the user control output by.

    The following code example sets Duration to 60 seconds and VaryByParams to a form POST or query string parameter named State. This code should be included before the code that extends the UserControl class.

    [PartialCaching(60, State, null, null)]
    

    <PartialCaching(60, State, null, null)>
    

See Also

Community Additions

ADD
Show:
© 2014 Microsoft