如何在asp.net中的userControl中使用OutputCache

发布于 2024-10-16 16:14:03 字数 1332 浏览 1 评论 0原文

我有一个 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 Duration=10 VaryByParam="none" %>

,并且当我浏览该页面时, 这个错误出来了

[NullReferenceException:对象 未设置对实例的引用 目的。] Content_SectionNews.Page_Load(对象 发送者、EventArgs e) 在 c:\Documents 中 和设置\管理员\我的 文档\Visual Studio 2005\项目\AnaweenNews.root\AnaweenNews\anaween 网站\内容\SectionNews.aspx.cs:127 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp、对象 o、对象 t、EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象 发送者、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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2024-10-23 16:14:03

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文