Come impostare la direttiva della cache di output sui controlli personalizzati senza codice in primo piano

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

Domanda

Ho scritto un controllo che eredita da System.Web.UI.WebControls.DropDownList e quindi non ho alcun codice davanti per questo controllo, ma voglio comunque impostare la direttiva OutputCache.Esiste un modo per impostarlo nel codice C#, ad esempio con un attributo o qualcosa del genere?

Spero in particolare di riuscire a replicare il VaryByParam proprietà

È stato utile?

Soluzione

Mi rendo conto che questa è una domanda incredibilmente vecchia ma merita ancora una risposta.

Quello di cui stai parlando non è un controllo utente, è un controllo personalizzato.Ciò che vuoi fare con OutputCache può essere fatto semplicemente con Context Cache.

Nel tuo codice in cui ottieni i dati e ti colleghi al tuo DropDownList fai qualcosa del genere:

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

Altri suggerimenti

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top