how to use OutputCache in a userControl in asp.net

1.3k views Asked by At

I have a aspx page that have this piece of code to load a usercontrol loaded from database

Control userControl = new Control();

userControl = LoadControl(userControlName);

((HiddenField)userControl.FindControl("HiddenFieldCategoryID")).Value = categoryID.ToString();

((HiddenField)userControl.FindControl("HiddenFieldNewsID")).Value = newsID.ToString();

((HiddenField)userControl.FindControl("HiddenFieldTypeID")).Value = typeID.ToString();

PlaceHolder3.Controls.Add(userControl);

and the ascx have an outputcache

<%@ OutputCache Duration=10 VaryByParam="none" %>

when i browse the page this error comes out

[NullReferenceException: Object reference not set to an instance of an object.] Content_SectionNews.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\AnaweenNews.root\AnaweenNews\anaween website\Content\SectionNews.aspx.cs:127 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.3615; ASP.NET Version:2.0.50727.3618

1

There are 1 answers

0
Ayman Habeb On BEST ANSWER

The type that returned from the LoadControl it will be PartialCachingControl, please follow the steps to how use the PartialCachingControl:

PartialCachingControl userControl = LoadControl(userControlName) as PartialCachingControl;

PlaceHolder3.Controls.Add(userControl);

if(userControl.CachedControl != null)
{
    ((HiddenField)userControl.CachedControl.FindControl("HiddenFieldCategoryID")).Value = categoryID.ToString();    

    ((HiddenField)userControl.CachedControl.FindControl("HiddenFieldNewsID")).Value = newsID.ToString();

    ((HiddenField)userControl.CachedControl.FindControl("HiddenFieldTypeID")).Value = typeID.ToString();
}