Cómo configurar la directiva de caché de salida en controles personalizados sin código delante

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

Pregunta

He escrito un control que hereda del System.Web.UI.WebControls.DropDownList Por eso no tengo ningún código delante para este control, pero aún quiero configurar la directiva OutputCache.¿Hay alguna forma de configurar esto en el código C#, digamos con un atributo o algo así?

En particular, espero poder replicar el VaryByParam propiedad

¿Fue útil?

Solución

Me doy cuenta de que esta es una pregunta increíblemente antigua, pero aún merece una respuesta.

De lo que estás hablando no es un control de usuario, es un control personalizado.Lo que desea hacer con OutputCache se puede hacer simplemente con Context Cache.

En su código donde obtiene los datos y los vincula a su DropDownList, haga algo como esto:

        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();

Otros consejos

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top