Caching a dynamically/programmatically added user control

时光毁灭记忆、已成空白 提交于 2019-12-11 06:28:07

问题


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.

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.

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>

The control control.ascx looks like this:

<%@ 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>

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.

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.

In this case, I use code like this:

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

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

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).

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'.

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?

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.


回答1:


Ah ha! Finally found another question on SO that helped

How to LoadControl a control that uses VaryByControl OutputCache, specifying values for properties

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)

to

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

Sorted it!



来源:https://stackoverflow.com/questions/12028912/caching-a-dynamically-programmatically-added-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!