public void Render(ViewContext viewContext, TextWriter writer) { Page page = new Page(); Control ctl = page.LoadControl(ViewPath); if (ctl == null) { throw new InvalidOperationException(string.Format("指定的用户控件 {0} 没有找到。", ViewPath)); } // 将用户控件放在Page容器中。 page.Controls.Add(ctl); PartialCachingControl mycachectl = ctl as PartialCachingControl;//如果有 写outputcache指令 Control temp = ctl; if (mycachectl != null) { Page_request.FastSetValue(page, viewContext.HttpContext.Request);//将当前的Request 写入当前页面 page.DesignerInitialize(); temp = mycachectl.CachedControl; } ViewUserControlBase myctl = temp as ViewUserControlBase; if (myctl != null) { myctl.ViewContext = viewContext; } HtmlTextWriter write = new HtmlTextWriter(writer, string.Empty); page.RenderControl(write); }
//动态加载用户控件 protected void Page_Init(object sender, EventArgs e) { this.Label1.Text = DateTime.Now.ToLongTimeString(); //动态加载用户控件,并返回PartialCachingControl的实例对象 PartialCachingControl pcc = LoadControl("WebUserControl.ascx") as PartialCachingControl; //如果用户控件的缓存时间大于60秒,那么重新设置缓存时间为10秒 if (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60)) { //设置用户控件过期时间 pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(10))); //设置缓存绝对过期 pcc.CachePolicy.SetSlidingExpiration(false); } Controls.Add(pcc); //将用户控件添加到页面中 }
private static PortalMasterView CreateMasterPrivate(string virtualPath) { Control control = Loader.LoadControl(virtualPath); PortalMasterView master = control as PortalMasterView; if (master == null) { PartialCachingControl pcc = control as PartialCachingControl; if (pcc != null) { master = pcc.CachedControl as PortalMasterView; } } if (master == null) { throw new ArgumentException("The control '" + virtualPath + "' is not of type '" + typeof(PortalMasterView).Name + "'.", "path"); } return(master); }
private static Tile CreateTilePrivate(TileDefinition tileDefinition) { Control control = Loader.LoadControl(tileDefinition.VirtualPath); Tile tile = control as Tile; if (tile == null) { PartialCachingControl pcc = control as PartialCachingControl; if (pcc != null) { tile = pcc.CachedControl as Tile; } } if (tile == null) { throw new ArgumentException("The control '" + tileDefinition.VirtualPath + "' is not of type '" + typeof(Tile).Name + "'.", "path"); } tile.LayoutId = tileDefinition.LayoutId; return(tile); }