缓存动态/编程方式添加用户控件 [英] Caching a dynamically/programmatically added user control

查看:112
本文介绍了缓存动态/编程方式添加用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解缓存,尤其是,部分使用高速缓存控制。

I'm trying to learn about caching, and in particular, partial caching using controls.

我的网站上运行某些网页慢,所以缓存尽可能会有所帮助。

My website is running slow on certain pages, so caching as much as possible will be helpful.

已经运行了一批从code实验,我已经在SO和其他各种谷歌搜索结果中,我遇到了一个问题,用动态添加控件。

Having run a number of experiments from code I have found on SO and various other Google results, I am running into an issue with dynamically added controls.

我已经建立了一个简单的页面,包含此code:

I have set up a simple page, containing this code:

<%@ Page Language="VB" Debug="true" %>
<%@ Register TagPrefix="controls" TagName="control" Src="~/test/control.ascx" %> 

<script runat="server">          
   Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

       Label2.Text = "Present Time: "
       Label2.Text += DateTime.Now.ToString()

   End Sub    
</script>
<html>
<body>
   <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">Output Caching</h2>
        <asp:Label ID="Label2" runat="server"></asp:Label>        
        <controls:control ID='control1' runat='server' /> 
        '------------------------------------------
        <hr />
        <div id='dyn2' runat='server' />   
    </div>
    </form>
</body>
</html>

控制 control.ascx 是这样的:

<%@ Control Language="VB" ClassName="control" %>
<%@ OutputCache Duration="60" VaryByParam="r" %>
<script runat="server">
    Sub Page_Load() Handles Me.Load
        controlContent.InnerHtml = "Control time: " & DateTime.Now.ToString()
    End Sub
</script>
<div id="controlContent" runat="server"></div>

这工作得很好,并给了我一个活的时间在页面上,而缓存的控制显示我这60秒后才会更新已经过去了一段时间,因为每的OutputCache 声明。

This works well, and gives me a "live" time in the page, whilst the cached control shows me a time which is only updated after 60 seconds has passed, as per the OutputCache declaration.

我可以看到,当我需要缓存页面的一部分,而该部分明确进入页面以&LT我如何使用此对任何应用程序;控制&GT; 标记。在的VaryByParam 选项对我有用的。 (我还没有调查 VaryByCustom是!)

I can see how I can use this for any application when I need to cache a part of a page and that part is explicitly entered into the page with a <controls> tag. The varyByParam option is useful to me too. (I've yet to investigate varyByCustom!)

然而,在某些情况下,我加载一个控制成页面,编程根据具体需要。

However, in some cases I am loading a control into a page, programmatically based on specific needs.

在这种情况下,我用code是这样的:

In this case, I use code like this:

Dim theResult As test_control2 = CType(LoadControl("~\test\control2.ascx"), test_control2)
dyn2.Controls.Add(theResult)

这是编程加上我的第二个测试控制,想象力题为 control2.ascx ID DYN2股利

This is programmatically adding my second test control, imaginatively entitled control2.ascx into the div with id "dyn2".

在控制无缓存指令的头,或者是code-背后,L一切工作正常,但我不能缓存它(除非我缓存整个页)。

With no cache directive header in the control, or it's code-behind,l everything works fine, but I can't cache it (unless I cache the entire page).

不过,如果我添加了缓存头按控制code以上,我得到这个错误:

However, if I add the cache header as per the control code above, I get this error:

Unable to cast object of type 'System.Web.UI.PartialCachingControl' to type 'test_control2'.

谷歌搜索似乎并没有帮助我很多这一点,并调查 PartialCachingControl 类型有引我到进一步的问题!

Googling doesn't seem to help me much with this, and investigating the PartialCachingControl types has lead me into further problems!

有人能告诉我应该做的,使我能够缓存这些控件?

Can someone tell me what should be doing to enable me to cache these controls?

如果它很重要,我在VB.net这个平台上的编码,也使用.NET 2.0,依此类推限制任何意见将AP preciated太(如果适用)。

If it matters, I am coding in VB.net and also using .NET 2.0, so any advice on limitations on this platform would be appreciated too, if applicable.

推荐答案

啊哈!最后发现,帮助在SO另一个问题

Ah ha! Finally found another question on SO that helped

<一个href=\"http://stackoverflow.com/questions/6574528/how-to-loadcontrol-a-control-that-uses-varybycontrol-outputcache-specifying-val\">How到LoadControl使用VaryByControl的OutputCache,对于性能指定值控制

基本上,我是加载控件了变化时,使用了错误的类型:

Basically, I was using the wrong Type when loading the control changin:

Dim theResult As test_control2 = CType(LoadControl("~\test\control2.ascx"), test_control2)
dyn2.Controls.Add(theResult)

Dim theResult As PartialCachingControl = DirectCast(LoadControl("~\test\control2.ascx"), PartialCachingControl)
dyn2.Controls.Add(theResult)

排序呢!

这篇关于缓存动态/编程方式添加用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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