我有一个aspx页面,其中包含这段代码来加载从数据库加载的用户控件
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有一个outputcache
<%@ OutputCache Duration=10 VaryByParam="none" %>
当我浏览页面时 出现这个错误
[NullReferenceException:Object 引用未设置为的实例 宾语。] Content_SectionNews.Page_Load(对象 发件人,EventArgs e)在c:\ Documents中 和设置\管理员\我的 Documents \ Visual Studio 2005 \项目\ AnaweenNews.root \ AnaweenNews \ anaween 网站\内容\ SectionNews.aspx.cs:127 错误帮助(IntPtr的 fp,Object o,Object t,EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object 发件人,EventArgs e)+35 System.Web.UI.Control.OnLoad(EventArgs的 e)+99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+627
版本信息:Microsoft .NET 框架版本:2.0.50727.3615; ASP.NET版本:2.0.50727.3618
答案 0 :(得分: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();
}