Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在前面没有代码的自定义控件上设置output cache指令_C#_Asp.net_Caching_Outputcache - Fatal编程技术网

C# 如何在前面没有代码的自定义控件上设置output cache指令

C# 如何在前面没有代码的自定义控件上设置output cache指令,c#,asp.net,caching,outputcache,C#,Asp.net,Caching,Outputcache,我已经编写了一个继承自System.Web.UI.WebControls.DropDownList的控件,因此我前面没有该控件的任何代码,但我仍然希望设置OutputCache指令。我想知道有没有办法在C代码中设置它,比如说使用属性之类的东西 我特别希望能够复制VaryByParam属性我意识到这是一个非常古老的问题,但仍然值得回答 Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); Response.Cache.SetCacheabi

我已经编写了一个继承自
System.Web.UI.WebControls.DropDownList
的控件,因此我前面没有该控件的任何代码,但我仍然希望设置OutputCache指令。我想知道有没有办法在C代码中设置它,比如说使用属性之类的东西


我特别希望能够复制
VaryByParam
属性

我意识到这是一个非常古老的问题,但仍然值得回答

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);
您所说的不是用户控件,而是自定义控件。您想要用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();
List listOfObjects=null;
//假设有一个对象列表。。。无论您使用哪种类型的数据都无关紧要
if(Context.Cache[“MyDataCacheKey”]==null)
{
//数据未缓存,请从数据库加载
listOfObjects=GetDataFromDB();
//将数据添加到上下文缓存,滑动过期时间为10分钟。
Add(“MyDataCacheKey”,listOfObjects,null,
System.Web.Caching.Cache.NoAbsoluteExport,
时间跨度从分钟(10.0),
System.Web.Caching.CacheItemPriority.Normal,null);
}
其他的
ListoObjects=(List)Context.Cache[“MyDataCacheKey”];
DropDownList1.DataSource=对象列表;
DropDownList1.DataBind();

我意识到这是一个令人难以置信的老问题,但它仍然值得回答

您所说的不是用户控件,而是自定义控件。您想要用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();
List listOfObjects=null;
//假设有一个对象列表。。。无论您使用哪种类型的数据都无关紧要
if(Context.Cache[“MyDataCacheKey”]==null)
{
//数据未缓存,请从数据库加载
listOfObjects=GetDataFromDB();
//将数据添加到上下文缓存,滑动过期时间为10分钟。
Add(“MyDataCacheKey”,listOfObjects,null,
System.Web.Caching.Cache.NoAbsoluteExport,
时间跨度从分钟(10.0),
System.Web.Caching.CacheItemPriority.Normal,null);
}
其他的
ListoObjects=(List)Context.Cache[“MyDataCacheKey”];
DropDownList1.DataSource=对象列表;
DropDownList1.DataBind();

响应对象不是控件的属性,只是父页面的属性,所以这不会只更改整个页面的缓存设置吗?响应对象不是控件的属性,只是父页面的属性,所以这不会只更改整个页面的缓存设置吗?