ASP.net如何输出缓存上的控件的公共属性一webusercontrol [英] ASP.net How to output cache a webusercontrol on controls public properties

查看:177
本文介绍了ASP.net如何输出缓存上的控件的公共属性一webusercontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web用户控件,它提供一些潜在的密集型数据计算,我想它是缓存,以便每个页面视图不会重新计算数据输出。它驻留在非常频繁浏览网页所以这是我得到它的工作很重要的权利!

I have a web user control, it serves some potentially intensive data calculations and I would like it to be output cached so that each page view doesn't recalculate the data. It resides on very frequently viewed pages so it's quite important I get it working right!

有关的背景下,它的使用在我们的商场:
http://www.scirra.com/arcade/action/93/8 -Bits亚军

For context, it's used on our arcade: http://www.scirra.com/arcade/action/93/8-bits-runner

点击统计,从这个webusercontrol生成的图表和统计数据。

Click on stats, the data for the graphs and stats are generated from this webusercontrol.

控制的开始如下:

public partial class Controls_Arcade_Data_ArcadeChartData : System.Web.UI.UserControl
{
    public int GameID { get; set; }
    public Arcade.ChartDataType.ChartType Type { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

现在我有难的是输出缓存需要依赖于两个GamID和图表类型。

Now the difficulty I'm having is the output cache needs to be dependant on both the GamID and the ChartType.

这是控制重复使用与游戏ID的类型和,我需要它为每个这些缓存,但我在努力寻找如何做到这一点的许多不同组合。

This control is re-used with many different combinations of GameID's and Types, I need it to create a cache for each of these but am struggling to find out how to do this.

例如,一个街机游戏可能会在游戏ID = 93 通和键入= GraphData ,另一个可能是游戏ID = 41 键入= TotalPlaysData ,另一个可能是游戏ID = 93 键入= TotalPlaysData 。这些都应该返回不同的数据,有不同的输出缓存。

For example, one arcade game might pass in GameID = 93 and Type = GraphData, another might be GameID = 41 and Type = TotalPlaysData and another might be GameID = 93 but Type = TotalPlaysData. These should all return different data and have different output caches.

该控件游戏页面有点像本上使用(该参数在codebehind实际设置)

The control is used on the games page sort of like this (the parameters are actually set in the codebehind)

<div>Total Plays:</div>
<div class="count"><Scirra:ArcadeChartData runat="server" GameID="93" Type="TotalPlays" /></div>
<br /><br />
<div>Total Guest Plays:</div>
<div class="count"><Scirra:ArcadeChartData runat="server" GameID="93" Type="TotalGuestPlays" /></div>

etc.

任何帮助AP preciated!我花了网上找了一段时间,它的不断涌现起来的东西,我需要解决,但找不出这一个。

Any help appreciated! I've spent a while looking online and it's kept coming up as something I need to solve but can't figure this one out.

修改

编辑:我已经试过加入这行来我的控制:
    &LT;%@的OutputCache时间=20VaryByControl =游戏ID;键入%>

I've tried adding this line to my control: <%@ OutputCache Duration="20" VaryByControl="GameID;Type" %>

但它只是引发错误对象引用未设置到对象的实例。上线,其中游戏ID 被设置在第一次使用控制ASPX页面上。

But it just throws the error Object reference not set to an instance of an object. on the line where GameID is being set for the first time on the ASPX page using the control.

推荐答案

在控制从输出缓存中检索,这不是实例化,你可以操纵一个实例;你刚刚得到控制生成的输出,而不是控制本身。例如,你可以不设置从code缓存控件属性后面,当你在你的问题说。的变化,通过属性应该被声明方式设置(使用前pressionBuilder也可能工作,虽然我还没有尝试过)。

When a Control is retrieved from the output cache, it's not instantiated as an instance that you can manipulate; you just get the output the Control generated, not the Control itself. For example, you can't set properties on a cached Control from code behind, as you said in your question. The vary-by properties should be set declaratively (using an ExpressionBuilder might also work, though I haven't tried it).

要看看背后是否控制已经从输出缓存中检索code,检查空:

To see in code behind whether a control has been retrieved from the output cache, check for null:

if (this.YourControlID != null) // true if not from cache
{
    // do stuff
}

即便如此告诫,控制输出缓存是有点古怪。

Even with that caveat, Control output caching is a bit quirky.

试试这个:

<%@ OutputCache Duration="20" VaryByControl="GameID;Type" Shared="true" %>

控制的输出由其与特定键相关联存储在输出高速缓存。随着共享=真正的,缓存关键是所有指定属性的值,与控件的ID在一起。如果没有共享=真正的,缓存键还包括页面类型,因此,产量将有所不同页面 - 这听起来并不像你想要什么。

The output of the Control is stored in the output cache by associating it with a certain key. With Shared="true", the cache key is the value of all specified properties, together with the Control's ID. Without Shared="true", the cache key also includes the Page type, so the output would vary by Page -- which doesn't sound like what you want.

如果你使用一个以上的页面上的控制,可以肯定的,如果你能使用在每一页上相同的ID,因为ID包含作为输出缓存键的一部分。如果你不能或者不使用不同的ID,你会得到控制输出的新副本在缓存中的每个唯一ID。如果具有不同ID的控件总是有不同的属性值,无论如何,这可能不是一个问题。

If you use the Control on more than one page, be sure to use the same ID on each page if you can, since the ID is included as part of the key for the output cache. If you can't or don't use different IDs, you will get a new copy of the Control's output in the cache for each unique ID. If the Controls with different IDs always have different property values anyway, that may not be an issue.

至于的OutputCache 指令的替代方法,可以在类声明的属性集:

As an alternative to the OutputCache directive, you can set an attribute on the class declaration:

[PartialCaching(20, null, "GameID;Type", null, true)]
public partial class Controls_Arcade_Data_ArcadeChartData : UserControl

这篇关于ASP.net如何输出缓存上的控件的公共属性一webusercontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆