後台動態修改iframe屬性 Loading pages in IFRAME dynamically from codebehind – ASP.NET

來源:互聯網
上載者:User

 

Click here for a Video version of this Article

 

 

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.NETframe1.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.A related article on Triggering an event in the Parent Page from within Page inside IFRAME Cheers !!!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.