开发者

how to use OutputCache in a userControl in asp.net

开发者 https://www.devze.com 2023-02-09 13:10 出处:网络
I have a aspx page that have this piece of code to load a usercontrol loaded from database Control userControl = new Control();

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.Con开发者_如何转开发trol.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


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消