Wrap the IFRAME Tag into a ASP.NET User Control

Introduced with the HTML 4.0 standard, the tag represents an inline page frame that points to a local or remote URL. The tag can be used to incorporate external

ITPro Today

October 26, 2004

2 Min Read
ITPro Today logo in a gray background | ITPro Today

Introduced with theHTML 4.0standard, the tag represents an inline page frame  that points to alocal or remote URL. The tag can be used to incorporate external ordynamically generated contents to your page. In ASP.NET, most HTML tags have amade-to-measure HTML server control. For example, the elementmaps to the HtmlInputText control. To transform a markup literal into a servercontrol, you just add the runat=server attribute: 

 

You can add the runat=serverattribute to any valid HTML tag, including custom and browser-specific tags.Some valid HTML tags have a specific server counterpart; some are renderedthrough a generic HTML server control named HtmlGenericControl. Specific HTMLserver controls have a programming interface that matches the set of attributesof the particular tag. The generic HTML server control features only theAttributes collection and the InnerText and InnerHTML properties. Wheninstantiated on the server, the tag is represented with aHtmlGenericControl object. 

 

You can programmatically setany visual attributes on the inline frame, and set the target URL, using theAttributes collection, as shown below. 

Frame.Attributes("src") = urlFrame.Attributes("style") = "border:solid 1px;"  

Although functional, thisprogramming approach is not very elegant indeed. A better approach entailsencapsulating the server tag into a user control.  

<%@ Control Language="VB" ClassName="IFrame" %>   

The layout of the usercontrol contains a server-side tag that is a protected resourceinvisible to the outside world. The object model you build on top of the usercontrol lets you decide which properties to expose, their names and syntax. Inthe above example, you can set the URL using a custom NavigateUrl method or theSrc property. The following sample page demonstrates the usage of the usercontrol. 

<%@ Page Language="vb" %><%@ Register TagPrefix="dino" TagName="IFrame" Src="iframe.ascx" %>            
             http://msdn.microsoft.com          

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like