zoukankan      html  css  js  c++  java
  • 【转】后台动态修改iframe属性 Loading pages in IFRAME dynamically from codebehind ASP.NET

     

     

    Most of us who develop Web Applications would have used an IFRAME during some stage of our lives. IFRAME's are an easy way by which you can embed another page within your original page such that you can show some important information like Stock position/Weather from another site without worrying about the changes happening to that site and updating the same. The Frame can also be used to show another page from your own application.

    ASP.NET also provides the option to have an IFRAME in our ASPX Pages. It can be with/without the "runat=server" attribute and does serve the purpose of embedding the page. The source for the frame can be set as follows:-

     

    <IFRAME id="frame1" src="SourcePage.extension / URL of the external Site" scrolling="auto">
    </IFRAME>


    However, in practical scenarios, we may want to load the page dynamically. In other words, we may want to specify the "src" attribute (the page which we want to show), dynamically. There is no straight forward way to do that and even if you add a "runat=server" attribute to it (though required in the work around provided below), you cannot access the "src" property directly.

    The workaround to do that is as follows:-

    1. Specify the "runat=server" attribute as follows in the ASPX Page:-

    <IFRAME id="frame1" scrolling="auto" runat="server">
    </IFRAME>

    2. In the codebehind, you may need to declare a HtmlGenericControl in the control declarations section as follows:-

    C#
    protected System.Web.UI.HtmlControls.HtmlGenericControl frame1; (not required if working with ASP.NET  2.0, 3.5 i.e. Visual Studio 2005, 2008)
     
    VB.NET
    Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl (not required if working with ASP.NET  2.0, 3.5 i.e. Visual Studio 2005, 2008)
     
    3. Then, you need to do a findcontrol to identify the control on the page and typecast it as follows:-
    C#
    HtmlControl frame1 = (HtmlControl)this.FindControl("frame1"); (not required if working with ASP.NET  2.0, 3.5 i.e. Visual Studio 2005, 2008)
     
    VB.NET
    Dim frame1 As HtmlControl = CType(Me.FindControl("frame1"), HtmlControl) (not required if working with ASP.NET  2.0, 3.5 i.e. Visual Studio 2005, 2008)

    Note: You can have different name for the Generic Control you define in the code behind, but for ease I keep both the same. The "frame1" referred in Find Control is the ID as declared in the ASPX Page.

    4. Thereafter, you will be able to access the src property as follows:-

     
    C#
    frame1.Attributes["src"] = "http://www.live.com" ;

     
    VB.NET
    frame1.Attributes("src") = "http://www.live.com" ;
    NOTE: Thanks PhOeNiX for providing the VB.NET equivalent.  I have added the same now.
    As you can see though I have hard-coded the URL you can assign it dynamically based on a condition or any other business logic.

    This serves the purpose of dynamically loading the page in the IFRAME.

    Cheers !!!
  • 相关阅读:
    Python学习笔记——基础篇【第二周】——解释器、字符串、列表、字典、主文件判断、对象
    HashMap、HashTable、ConcurrentHashMap、TreeMap、LinkedHashMap、WeakHashMap区别
    IntelliJ IDEA 控制台中文乱码解决方案
    Java 使用 Redis
    redis.conf 配置项说明
    虚拟机性能监控与故障处理工具
    图解Git
    常用git命令
    设计模式的类型
    使用mybatis插件自动生成代码以及问题处理
  • 原文地址:https://www.cnblogs.com/5tao/p/1944632.html
Copyright © 2011-2022 走看看