用户自定义控件缓存的问题

kane 2003-12-05 05:31:59
在aspx文件中引用控件如下
<uc1:coursemenu id="Coursemenu1" CourseID=<%#CourseID%> runat="server"></uc1:coursemenu>没有问题
控件有一个参数CourseID,
如果在后台cs代码中使用如下:
Control control = LoadControl("controls/coursemenu.ascx");
((coursemenu)control).CourseID = CourseID;
也是没有问题的。
但是在acsx中加入语句
<%@ OutputCache Duration="60" VaryByParam="id" %>
在aspx文件中使用没有问题,但是在后台代码中使用该控件发生错误,
说是无效的转换,请各位帮忙
...全文
465 40 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
kane 2003-12-11
  • 打赏
  • 举报
回复
谢谢思归大大,问题解决了,确实是需要先把form添加到page's controls里,才可以在form里添加用户控件
saucer 2003-12-10
  • 打赏
  • 举报
回复
move

this.Controls.Add(form);

to the top
kane 2003-12-10
  • 打赏
  • 举报
回复
如果直接用this.Controls.Add(c),可以引用到((PartialCachingControl)c).CachedControl的缓存对象,如果是form.Controls.Add(c),就无法引用到缓存对象。

但是不把用户控件添加到form表单中,用户控件里将不能包含web控件
kane 2003-12-10
  • 打赏
  • 举报
回复
我把PageTemplate里的代码整理了,只保留了添加用户控件的部分做调试。
有这样一段代码
private HtmlForm GenerateHtmlForm()
{
HtmlForm form = new HtmlForm();
AddControlsFromDerivedPage(form);
return form;
}

protected virtual void AddControlsFromDerivedPage(HtmlForm form)
{
//save the validators
IValidator[] arrPageValidators = new IValidator[this.Validators.Count];
this.Validators.CopyTo(arrPageValidators,0);

int count = this.Controls.Count;
for(int i=0; i<count; ++i)
{
System.Web.UI.Control ctrl = this.Controls[0];
form.Controls.Add(ctrl);
this.Controls.Remove(ctrl);
}

//add the saved validators to the page
foreach(IValidator myValidator in arrPageValidators)
{
this.Validators.Add(myValidator);
}
}
是为了添加一个form表单,把用户控件添加到form表单里
protected override void OnInit(System.EventArgs e)
{
BuildPage(GenerateHtmlForm());
base.OnInit(e);
}

protected void BuildPage(HtmlForm form)
{
////////////////////////////////////////////////////////
// Build the page and include the generated form

AddHtmlHeader();

Control c = LoadControl("web1.ascx");
// Controls.Add(c);
// form.Controls.Add(c);
this.Controls.Add(c);
//Response.Write(c.GetType().Name);
if (c is PartialCachingControl && ((PartialCachingControl)c).CachedControl != null)
{
web1 mc = (web1)((PartialCachingControl)c).CachedControl;
mc.CourseID = String.Format("{0}****{1}", Request.Params["id"],"xxxxxx");
}

this.Controls.Add(form);

AddFooter();
}
kane 2003-12-10
  • 打赏
  • 举报
回复
首先恭喜思归老大荣升大版主
saucer 2003-12-09
  • 打赏
  • 举报
回复
there are too much code to sort out, I would suggest you to write something smaller, for example, get rid of most of the code in pagetemplate, and make the cached control work first, then add piece by piece back
kane 2003-12-09
  • 打赏
  • 举报
回复
showcourse.aspx?id=5,showcourse.aspx是这样被请求的,showcourse.aspx的cs是继承pagetemplate,在pagetemplate里load web1.ascx,在web1.ascx里加的cache语句是
<%@ OutputCache Duration="60" VaryByParam="id" %>,这样可以吗?
kane 2003-12-08
  • 打赏
  • 举报
回复
上面被注释掉的loadcontrol里的controls/web1.ascx控件,就是按照你给的1.ascx写的,区别就是换了个名字
kane 2003-12-08
  • 打赏
  • 举报
回复
public class PageTemplate : System.Web.UI.Page
{

public PageTemplate()
{
}

protected override void OnInit(System.EventArgs e)
{

BuildPage(GenerateHtmlForm());
base.OnInit(e);
}

#region 向web页面中添加控件

protected void BuildPage(HtmlForm form)
{
////////////////////////////////////////////////////////
// Build the page and include the generated form

AddHtmlHeader();


if(ShowCourseMenu)
{
AddCourseMenu(form);
form.Controls.Add(new LiteralControl( @"
</td>
<td vAlign=top width=18></td>
</tr>
</table>
"));
}

AddHeader(form);

this.Controls.Add(form);

AddFooter();
}

private HtmlForm GenerateHtmlForm()
{
HtmlForm form = new HtmlForm();
AddControlsFromDerivedPage(form);
return form;
}

protected virtual void AddControlsFromDerivedPage(HtmlForm form)
{
//save the validators
IValidator[] arrPageValidators = new IValidator[this.Validators.Count];
this.Validators.CopyTo(arrPageValidators,0);

int count = this.Controls.Count;
for(int i=0; i<count; ++i)
{
System.Web.UI.Control ctrl = this.Controls[0];
form.Controls.Add(ctrl);
this.Controls.Remove(ctrl);
}

//add the saved validators to the page
foreach(IValidator myValidator in arrPageValidators)
{
this.Validators.Add(myValidator);
}
}
#endregion
protected void AddCourseMenu(HtmlForm form)
{
form.Controls.AddAt(0, new LiteralControl( @"
</td>
<td width=21> </td>
<td vAlign=top width=550>
"));

Control control = LoadControl("controls/coursemenu.ascx");
/* ((coursemenu)control).CourseID = CourseID;
*/

/**/ form.Controls.AddAt(0, control);

coursemenu cm = null;

if (control is coursemenu)
cm = (coursemenu)control;
else if (control is PartialCachingControl && ((PartialCachingControl) control).CachedControl != null)
cm = (coursemenu)((PartialCachingControl) control).CachedControl;

if (cm != null)
{
cm.CourseID = CourseID;
cm.PageName = PageName;
}
/*
Control c = LoadControl("controls/web1.ascx");
// Controls.Add(c);
form.Controls.Add(c);
//Response.Write(c.GetType().Name);
if (c is PartialCachingControl && ((PartialCachingControl)c).CachedControl != null)
{
web1 mc = (web1)((PartialCachingControl)c).CachedControl;
mc.CourseID = String.Format("{0}****{1}", Request.Params["id"],"xxxxxx");
}
*/
form.Controls.AddAt(0, new LiteralControl( @"
<table cellSpacing=0 cellPadding=0 width=776 align=center border=0>
<tr>
<td width=11> </td>
<td vAlign=top width=176>
"));
}
kane 2003-12-08
  • 打赏
  • 举报
回复
to:思归

这两天网络出了故障没能上网,来晚了。

我按照上面的代码也做了测试,按照上面的代码做没有问题,但是如果在我的page template里load控件的话,还是无法得到((PartialCachingControl)c).CachedControl所cache的控件
saucer 2003-12-08
  • 打赏
  • 举报
回复
>>>((PartialCachingControl)c).CachedControl always is null

that means the control is the cached version, i.e., your dependency is wrong, the cached version is not invalidated when you change id
kane 2003-12-08
  • 打赏
  • 举报
回复
在新建的工程里web1.ascx测试没有问题,在pagetempleta里load的web1.ascx,
((PartialCachingControl)c).CachedControl always is null
kane 2003-12-08
  • 打赏
  • 举报
回复
Control c = LoadControl("controls/web1.ascx");
form.Controls.Add(c);
if (c is PartialCachingControl && ((PartialCachingControl)c).CachedControl != null)
{
web1 mc = (web1)((PartialCachingControl)c).CachedControl;
mc.CourseID = String.Format("{0}****{1}", Request.Params["id"],"xxxxxx");
}

((PartialCachingControl)c).CachedControl always is null
saucer 2003-12-08
  • 打赏
  • 举报
回复
>>>上面被注释掉的loadcontrol里的controls/web1.ascx控件

my question is, did "web1.ascx" work as expected?
saucer 2003-12-06
  • 打赏
  • 举报
回复
the following works for me, and if you change the id value for "show.aspx?id=12", you should see the output from the usercontrol changes too

1.ascx:
<%@ Control Language="C#" Inherits="MyControl" src="1.ascx.cs" %>
<%@ OutputCache Duration="30" VaryByParam="id" %>

1.ascx.cs:
using System;
using System.Web;
using System.Web.UI;

public class MyControl : UserControl
{
string sID;

public string CourseID
{
get { return sID;}
set {sID = value;}
}

protected override void Render(HtmlTextWriter output)
{
output.Write(String.Format("Inside ascx:{0} {1}", DateTime.Now, CourseID));
}
}

show.aspx:

<%@ Reference Control="1.ascx" %>
<script language="C#" runat=server>
void Page_Load(Object o, EventArgs e)
{
Control c = LoadControl("1.ascx");
Controls.Add(c);
//Response.Write(c.GetType().Name);
if (c is PartialCachingControl && ((PartialCachingControl)c).CachedControl != null)
{
MyControl mc = (MyControl)((PartialCachingControl)c).CachedControl;
mc.CourseID = String.Format("{0}****{1}", Request.Params["id"],"xxxxxx");
}
}
</script>
kane 2003-12-06
  • 打赏
  • 举报
回复
我主要是做了一个page template,让其他的页面继承自该tempalte,用户控件里放了一个菜单,其中有些动态的东西需要由id来决定
Naola2001 2003-12-06
  • 打赏
  • 举报
回复
mark
saucer 2003-12-06
  • 打赏
  • 举报
回复
don't make so complicated, inside your ascx file code behind, use Page.Request.Params["id"] to set your courseid value
kane 2003-12-06
  • 打赏
  • 举报
回复
有没有哪位能将用户控件缓存到底是怎么实现、framework在底层做了哪些工作讲讲
kane 2003-12-06
  • 打赏
  • 举报
回复
现在有这么个链接show.aspx?id=5
在show.aspx中使用了1.ascx这个用户控件,在show.aspx.cs文件中取到id,把这个id的值付给1.ascx一个courseid属性。所以我的用户控件1.ascx希望根据show.aspx的参数id来缓存这个用户控件,所有在show.aspx.cs中load这个用户控件,同时将这个id的值付给控件的courseid属性。
如果不要求控件缓存的话,control is coursemenu
但是如果要求控件进行缓存,(coursemenu)((PartialCachingControl) control).CachedControl始终都是undefined value,所以引用到控件的实例,无法给courseid赋值,请思归大侠、吴旗娃等高人帮忙
加载更多回复(20)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧