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

JavaScript Editor jscript editor     Web designer 



Main Page

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

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:

    В CopyCode imageCopy Code
    <%@ 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.

    C#В CopyCode imageCopy Code
    [PartialCaching(60, null, State, null)]

    Visual BasicВ CopyCode imageCopy Code
    <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.

    В CopyCode imageCopy Code
    <%@ 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.

    C#В CopyCode imageCopy Code
    [PartialCaching(60, State, null, null)]

    Visual BasicВ CopyCode imageCopy Code
    <PartialCaching(60, State, null, null)>

See Also



JavaScript Editor jscript editor     Web designer