中继隐藏页面RelayPage是不会在浏览器中显示出来的,它存在的目的是为了辅助其它页面完成特定的功能,而如果不使用中继隐藏页面,那么这些特定功能的实现可能是很繁琐或拙劣的。下面通过两个例子来看看。
(1) 显示数据库中的图片
Image控件只能通过其ImageUrl来指定显示的图片,如果图片存放于数据库中,就不好处理了,我们可以把图片的数据读到内存中,但是Image控件不能绑定到内存数据和流,那么如何解决了?有人说可以先把图片数据在本地保存为一个临时文件,然后让Image控件的ImageUrl指向此文件,当然可以这么做,可是这种实现太不优雅,而且在图片众多的时候,需要频繁的与硬盘交互,而且对这些临时图片文件的管理也成问题。
更好的解决办法是,使用一个中继页面,在该页面的Page_Load方法中把图片数据从数据库读出,然后保存到Response.OutputStream到中。源码如下:
private
void
Page_Load(
object
sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/39d7d940c8c2f194345d303509c06b40.gif)
{
string thePicID = this.Request.QueryString["ID"] ;
BinaryInformationDetail picInfo = (BinaryInformationDetail)DataEntrance.GetAObjectEspecial(typeof(BinaryInformationDetail) ,thePicID) ;
![](https://i-blog.csdnimg.cn/blog_migrate/15b1c5de62d0b694d97c6d1b46298fcf.gif)
MemoryStream stream = new MemoryStream(picInfo.Data) ;
Bitmap bmp=new Bitmap(stream);
bmp.Save(this.Response.OutputStream ,System.Drawing.Imaging.ImageFormat.Jpeg) ;
}
(2) 点击一个HyperLink需要输出一个脚本在客户端执行,由于HyperLink没有点击事件触发,只有一个url链接,所以无法向Response写入脚本。替代的方法是使用RelayPage,让HyperLink的url指向一个RelayPage,而在RelayPage中Page_Load方法中向Response输出脚本。
private
void
Page_Load(
object
sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/39d7d940c8c2f194345d303509c06b40.gif)
{
PMS.Common.SessionContentInterPages sessionPages = new PMS.Common.SessionContentInterPages(this.Session) ;
sessionPages.MainFormRefreshInformation.RefreshType = RightFrameRefreshType.ForPic ;
sessionPages.MainFormRefreshInformation.theTargetID = this.Request.QueryString["ID"].ToString() ;
//向Response中写入脚本!
RefreshMapHelper.RefreshMap(this.Response ,MapRefreshType.ForPicLocation ,this.Request.QueryString["ID"].ToString() ,false) ;
EnterpriseServerBase.XWeb.PublicWebFunction.RefreshFrameInAncestorPage(this.Response ,"MainForm" ,"framedown" , "PicManage/PicDetailForm.aspx",true) ;
}
一般只需要使用中继隐藏页面的Page_Load方法就可以了。需要注意,有的中继隐藏页面需要在Page_Load执行完后就被关闭,如上面的示例2。