开发者

How to cache separate instances of user control depending upon a property value

I've an asp.net user control called UC_Test which has got a public property exposed called param1.It is hosted in aspx pages A and B.

On page A, param1 is set to 10 and on page B param1 is set to 20. Depending on the param value, the fo开发者_运维知识库rmatting of user control is controlled.

I want page A and page B to cache separates instances of user controls.

As okw has pointed out, I want to use caching for performance reasons and not for persisting values.Please let me know how do i achieve this.

Can I use Shared=false for this purpose?for example?

Thanks for reading.


This sounds like an ideal candidate for the VaryByCustom output cache directive. You can just use the value of your property to determine the difference between the cached versions.

See MSDN for the full details: http://msdn.microsoft.com/en-us/library/ms550239.aspx


The most straightforward way is to use a HiddenField that holds the appropriate value. Read it back from the page during a form submission or postback call.


You could do something like:

public int MyProperty
{
    get
    {
        return (int)ViewState[this.UniqueID + "_MyProperty"];
    }

    set
    {
        ViewState[this.UniqueID + "_MyProperty"] = value;
    }
}

ViewState in this scenario being interchangable with HttpContext.Current.Cache or HttpContext.Current.Session. You just need to make sure to check for nulls or whatever.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜