앞에 코드 없이 사용자 정의 컨트롤에 출력 캐시 지시어를 설정하는 방법

StackOverflow https://stackoverflow.com/questions/78974

문제

나는 System.Web.UI.WebControls.DropDownList 따라서 이 컨트롤 앞에 코드가 없지만 여전히 OutputCache 지시어를 설정하고 싶습니다.C# 코드에서 속성이나 그와 유사한 것을 설정할 수 있는 방법이 있나요?

특히 복제할 수 있기를 바라고 있습니다. VaryByParam 재산

도움이 되었습니까?

해결책

나는 이것이 엄청나게 오래된 질문이라는 것을 알고 있지만 여전히 대답할 가치가 있습니다.

당신이 말하는 것은 사용자 컨트롤이 아니라 사용자 정의 컨트롤입니다.OutputCache로 수행하려는 작업은 컨텍스트 캐시를 사용하여 간단하게 수행할 수 있습니다.

데이터를 가져오고 DropDownList에 바인딩하는 코드에서 다음과 같이 수행합니다.

        List<Object> listOfObjects = null;
//assuming a List of Objects... it doesn't matter whatever type of data you use
        if (Context.Cache["MyDataCacheKey"] == null)
        {
            // data not cached, load it from database
            listOfObjects = GetDataFromDB();
//add your data to the context cache with a sliding expiration of 10 minutes.
            Context.Cache.Add("MyDataCacheKey", listOfObjects, null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                TimeSpan.FromMinutes(10.0),
                System.Web.Caching.CacheItemPriority.Normal, null);
        }
        else
            listOfObjects = (List<Object>)Context.Cache["MyDataCacheKey"];

        DropDownList1.DataSource = listOfObjects;
        DropDownList1.DataBind();

다른 팁

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top