2011-02-10 39 views
1

使用的OutputCache在用户控件我有这段代码加载从数据库如何在asp.net

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

和ASCX加载的用户控件一个aspx页面有一个OutputCache

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

当我浏览的页面 此错误出来

[NullReferenceException异常:对象 参考不设置到 对象的实例] Content_SectionNews.Page_Load(对象 发件人,EventArgs e)如C:\文件 和设置\管理员\我 文档\的Visual Studio 2005 \项目\ AnaweenNews.root \ AnaweenNews \ anaween 网站\内容\ SectionNews.aspx.cs:127 错误帮助(IntPtr的 错误未将对象,对象T,EventArgs的) +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(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)627

版本信息:微软.NET框架 版本:2.0.50727.3615; ASP.NET版本:2.0.50727.3618

+0

可能重复[什么是在.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2011-02-19 10:13:54

回答

2

从LoadControl这将是PartialCachingControl返回的类型,请按照步骤如何使用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(); 
}