Click here to Skip to main content
15,355,659 members
Articles / Web Development / ASP.NET
Posted 9 Feb 2004


67 bookmarked

Passing parameters to an ASP.NET page running in an IFRAME

Rate me:
Please Sign up or sign in to vote.
3.45/5 (31 votes)
9 Feb 20044 min read
How to pass parameters to an aspx page that is hosted within an IFrame. Also how to handle postback issues in a modal window.


I am building a content management intranet for my company. The website is based on DotNetNuke, and uses the excellent FreeTextBox control in order to enter HTML into the articles. One of the best features of FreeTextBox is that you can easily add your own buttons and functionality to the control. For instance, the built-in function for adding images didn't meet my needs. My images are stored in SQL server, and require a custom src property in order to retrieve the image. No problem - I added a new button to the control, and built a small aspx page that opens in a modal (popup) window. The popup window allows the user to choose an image to insert, or upload a new image to be inserted. But that's where my problems began.

The Problem

If you've never tried to run an aspx page in a modal window before, then I've probably already lost your attention. But if you have, then you've most likely run into a rather odd problem. Just opening and running an aspx page in a modal window works fine, but if you need to do anything that causes a postback, a funny thing happens. The page opens up in a new window. Worse yet, the new window that opens doesn't have any of the data that you just submitted. The whole thing is a total wash. And JavaScript isn't terribly helpful. What to do?

Enter the IFrame

The trick to solving this problem is to wrap the aspx page in a "doorway" page. That is, you make another page that contains an <IFrame> tag with its source page set to your aspx page. The aspx page then behaves normally within the given frame. Here is an example:

<iframe src="Pages/multiforms.aspx" 
  name="embeddedFrame" width="100%" height="100%" />

Great! Ok, so chances are that at this point you are good to go. But there is one problem left - what if you (like I) needed to pass a parameter to the aspx page? In my example above, I needed to pass the article number of the page I was working on, so that the page I was calling would know which images to display. When calling the aspx page, I would do something like this:

But how do pass the parameters from the host page to the IFrame? Well, as the old saying goes, "You can't get there from here". Or can you?

The Solution

Enter the Literal control. That's the little friend that saves the day in this case. Here is how it works. First, create your doorway page (an aspx page) and drop a Literal control on it.

<form id="Form1" method="post" runat="server">
   <asp:Literal id="Literal1" runat="server"></asp:Literal>

In the code behind (or in the script section), load up the literal control with the IFrame tag, along with the parameters you need to pass:

Private Sub Page_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
    Dim myURL As String = Request.Url.ToString() 'Get the URL
    'Does the URL contain parameters?
    Dim ParamStart As Integer = myURL.IndexOf("?") 
    'If the URL has parameters, then get them. If not, return a blank string 
    Dim Params As String = IIf(ParamStart > 0, _
      myURL.Substring(ParamStart, myURL.Length - ParamStart), String.Empty)
    If (Request.Params("Page") Is Nothing) _
              Or (Request.Params("Page") = "") Then
        'No page was specified to load in the IFrame, so let the user know
        Literal1.Text = "Error: No page was specified in the parameters."
        'Return the IFrame, and add the parameters 
        'to the called (src) web page
        Literal1.Text = "<IFRAME NAME='embeddedFrame'" & _ 
          " WIDTH='100%' HEIGHT='100%' SRC='" & _
          Request.Params("Page") & Params & "' />"
    End If
End Sub

The code above is pretty short and well documented, but here it is in a nutshell. We grab the URL passed to the "doorway" page. The "Page" parameter contains the name of the aspx page we wish to load into the IFrame. If there are any additional parameters, we get those and store them in a string called Params. Then, using the Literal control, we dynamically create an IFrame tag. The trick here is, now we can create the IFrame with a src value that contains not only the name of the aspx page we want to load in the IFrame, but we can also add the passed parameters to that page. Cool.

Calling The Page

Now that we have our doorway page, we easily call the aspx page needed, and pass it the necessary parameters, all in one shot. In the example above, I was attempting to call the selectimage.aspx page and pass it an article number. Now, in order to make that work, I do this:

Where To Go From Here

That's pretty much it, although there is room for tweaking left, depending on how thorough you'd like to be. For example, you could easily turn this code into a simple control that could be dropped on a page, or even turn it into a base page to be inherited from. Now that you know the trick, all of these things are rather simple to do, and I leave those options to you to explore in more depth. If you are concerned about passing query strings in plain text as above, there was an excellent article on using secure query strings here.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Todd Davis
Systems Engineer Virtual RadioLogic
United States United States
Todd Davis has been working in web and application development for several years, using Silverlight, ASP.NET, VB.NET, C#, C++ and Javascript, as well as a great deal of work with SQL server and IIS.

He currently works for Virtual Radiologic in Eden Prairie, MN, however he is better known for his varied work in the open source community, especially the DotNetNuke project for which he provided several world-renowned training videos and modules. A huge advocate of open source and open knowledge sharing, everything on his website ( is always offered for free.

Whenever he is not actively coding at his laptop (a rarity to be sure), he can be found woodworking, walking with his wife and kids, or motoring along the back roads of MN on his Harley Davidson Fatboy.

Comments and Discussions

GeneralMy vote of 5 Pin
winny8716-May-13 23:19
Memberwinny8716-May-13 23:19 
Questioniframe scroll event Pin
ammiraju.t27-Feb-13 2:55
Memberammiraju.t27-Feb-13 2:55 
QuestionHelp Pin
Member 867650724-Feb-12 23:47
MemberMember 867650724-Feb-12 23:47 
GeneralUsing Session Pin
sameerazazi876-Aug-10 14:03
Membersameerazazi876-Aug-10 14:03 
GeneralRe: Using Session Pin
Todd Davis6-Aug-10 14:25
MemberTodd Davis6-Aug-10 14:25 
GeneralRe: Using Session Pin
sameerazazi876-Aug-10 18:29
Membersameerazazi876-Aug-10 18:29 
GeneralThanks! Pin
rax_s21-Nov-08 9:31
Memberrax_s21-Nov-08 9:31 
GeneralPassing Values Pin
motasem1114-Aug-08 3:16
Membermotasem1114-Aug-08 3:16 
QuestionCustomise IFrame src Pin
zarana shah30-Jul-08 21:56
Memberzarana shah30-Jul-08 21:56 
Questionhi Pin
bunny.varun19-Apr-08 23:43
Memberbunny.varun19-Apr-08 23:43 
GeneralUse IFrame Control Attributes to set src PinPopular
pmannino28-Nov-07 10:58
Memberpmannino28-Nov-07 10:58 
GeneralPlacing more than one iframe Pin
cmrhema24-Aug-07 21:31
Membercmrhema24-Aug-07 21:31 
GeneralIFrame in ASP.NET Pin
.NET- India 25-Jul-07 2:55
Member.NET- India 25-Jul-07 2:55 
GeneralUsing this inside DNN to embed ASP.NET E-mail Pin
Kashar20-Jul-07 5:53
MemberKashar20-Jul-07 5:53 
GeneralRe: Using this inside DNN to embed ASP.NET E-mail Pin
Todd Davis20-Jul-07 7:17
MemberTodd Davis20-Jul-07 7:17 
QuestionGetting Parameters from an IFrame Pin
estrga18-Jul-07 4:29
Memberestrga18-Jul-07 4:29 
AnswerRe: Getting Parameters from an IFrame Pin
Todd Davis18-Jul-07 4:44
MemberTodd Davis18-Jul-07 4:44 
GeneralRe: Getting Parameters from an IFrame Pin
estrga18-Jul-07 5:00
Memberestrga18-Jul-07 5:00 
GeneralLiteral Control Pin
pganathe3-Apr-07 20:25
Memberpganathe3-Apr-07 20:25 
GeneralRe: Literal Control Pin
parama dharmika15-May-07 4:01
Memberparama dharmika15-May-07 4:01 
GeneralRe: Literal Control Pin
pganathe15-May-07 19:39
Memberpganathe15-May-07 19:39 
Questionhow can set the height of ifram Pin
Member 156050216-Feb-07 20:53
MemberMember 156050216-Feb-07 20:53 
GeneralThanks Pin
.NETWannabe25-Oct-06 5:03
Member.NETWannabe25-Oct-06 5:03 
QuestionHow to make this ATLAS enabled? Pin
Dsypher18-Jul-06 8:08
MemberDsypher18-Jul-06 8:08 
Generalbase target Pin
shubie29-Sep-05 15:13
Membershubie29-Sep-05 15:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.