locked
Please Help - Problem with caching user control RRS feed

  • Question

  • User-7685681 posted
    Hi, I have a user control that is dynamically loaded on an aspx page. Everything works fine until I add <%@ OutputCache Duration="120" VaryByParam="entityID" %> to the user control. After I add the above directive, I get the following error where I try to declare the user control in the aspx page. System.InvalidCastException: Specified cast is not valid. This is how I am adding the user control (TableView.ascx) to the aspx page. TableView tblView = (TableView) Page.LoadControl("TableView.ascx"); tblView.ENTITYID = entityid; pnlTableView.Controls.Add(tblView); Can anyone tell me the mistake I am doing here. Your help is greatly appreciated. I want to cache multiple versions of the user control based on the parameter entityID of the control. Raj.
    Tuesday, June 8, 2004 5:23 PM

All replies

  • User43974391 posted
    Followings casting needs may be causing the problem: -- The return type of LoadControl() method is System.Web.UI.Control type. -- LoadControl() method expects System.Web.UI.Control as input type. In your code this is not the case. Perhaps you can try something like: Control _ControlTableView = (Control)Page.LoadControl("TableView.ascx"); ((TableView)_ControlTableView).ENTITYID = entityid; pnlTableView.Controls.Add(_ControlTableView); Though this does not answer how OutputCache introduced the problem.
    Wednesday, June 9, 2004 7:57 AM
  • User-7685681 posted
    Hi, Thanks for your reply. But I already tried that, and even then I got the same error but at the line ((TableView)_ControlTableView).ENTITYID = entityid; since I am doing the casting here. Is it true that one cannot cache a dynamically added user control that has properties? I would really want to be able to cache my user control. Raj.
    Wednesday, June 9, 2004 9:21 AM
  • User43974391 posted
    Hi, I tried to simulate the problem and I faced the same exception. The user control works fine, as long as the OutputCache is not set in the user control. But after seting it for caching it gives the InvalidCastException. And here is the solution along with the real cause of the problem. I am pasting contents of 4 different files to simulate the exact solution. Its been a real good learning for me. Cause of Exception: When caching is NOT enabled, everything will work fine. No casting issues will be faced. But when caching is enabled the actual type of object returned is PartialCachingControl. This is not the same as the type that Page_Load is expecting. So, when caching is enabled, some extra checks and casting needs to be done. Following is the working code for the same.

    Example Code: UCtlCache.ascx <%@ Control Language="c#" AutoEventWireup="false" Codebehind="UCtlCache.ascx.cs" Inherits="WebApp.UCtlCache" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%> <%@ OutputCache Duration="10" VaryByParam="*" %> <asp:Label id="lblOutText" runat="server" Width="184px"></asp:Label> Example Code: UCtlCache.ascx.cs namespace WebApp { using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; /// <summary> /// Summary description for UCtlCache. /// </summary> public class UCtlCache : System.Web.UI.UserControl { protected System.Web.UI.WebControls.Label lblOutText = new Label(); protected string _OutText; // Control's exposed property to set the visible text. public string OutText { get { return _OutText; } set { _OutText = value; } } private void Page_Load(object sender, System.EventArgs e) { // Set the label's value here. lblOutText.Text = _OutText; } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } } Example Code: TestUCtlCache.aspx <%@ Page language="c#" Codebehind="TestUCtlCache.aspx.cs" AutoEventWireup="false" Inherits="WebApp.TestUCtlCache" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <title>TestUCtlCache</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> </form> Example Code: TestUCtlCache.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebApp { /// <summary> /// Summary description for TestUCtlCache. /// </summary> public class TestUCtlCache : System.Web.UI.Page { Control _ControlUCtlCache; const string _UCtlCacheASCX = "UCtlCache.ascx"; private void Page_Load(object sender, System.EventArgs e) { // Dynamically load the UCtlCache control. UCtlCache _UCtlCache = null; _ControlUCtlCache = Page.LoadControl(_UCtlCacheASCX); Page.Controls.Add(_ControlUCtlCache); if(_ControlUCtlCache is UCtlCache) { _UCtlCache = (UCtlCache)_ControlUCtlCache; } else if (_ControlUCtlCache is PartialCachingControl && ((PartialCachingControl)_ControlUCtlCache).CachedControl != null) { _UCtlCache = (UCtlCache)((PartialCachingControl)_ControlUCtlCache).CachedControl; } if(_UCtlCache != null) { _UCtlCache.OutText = System.DateTime.Now.ToString(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
    Thursday, June 10, 2004 5:40 AM
  • User-7685681 posted
    Hi Dash, Yes, what you said is right. I am able to get away with that error and I really learnt about caching a user control from you. Thankyou very much for your valuable time. But now I get some other error because of this caching thing. For that I need to explain to you about my application. I am using Master Pages (wilsonmasterpages) for my web app. If you are familiar with master pages then great, if not I will try to explain as simply as I can. Hope you would understand. I create a template (a user control, for example Layout.ascx) that defines the layout of the web app. I divide this ascx page into several regions with different IDs. I reference this template file (using masterpages) in the aspx pages and I add content whereever I need to in this page using the region IDs. So no aspx page will have form tags except the Layout.ascx template user control. In such an aspx page I am now dynamically loading another user control. Before using Caching, I get no error. But after I enabled Caching and using your help I am able to avoid the first error. But when I run the app now, its saying that the controls I have in this user control must be inside a form tag with runat=server. And if I add a form tag in the user control, I get an error saying that there cannot be two form tags with runat=server. I think I am stuck again. Raj.
    Thursday, June 10, 2004 12:39 PM
  • User-230561051 posted

    hello,

    3 years later and this absolutely saved my behind.

    thank you so much!

    Wednesday, June 27, 2007 2:56 AM
  • User1641338112 posted

    It's now March 2008 and this has really helped me. Good one!

    Tuesday, March 18, 2008 1:27 AM
  • User1269398860 posted

    Thanx Dash...your solution was worth.

    I also noticed....if you had a requirement to cache the Usercontrols, the containing page which would host the control, could be cached. In this event the Usercontrol need not be cached enabling the webpage to leverage the caching feature. The above was noticed for a content page in a master page scenario.

    Thanx,

    Tushar M.

    Saturday, June 14, 2008 3:56 PM