Monday, September 1, 2008

Passing QueryString to IFrame

Page Redirection

If you have a page contains an iframe which contains Page1.aspx, and you want to redirect to Page2.aspx from Page1.aspx, you cannot just use response.redirect in Page1.aspx.vb, you could use the following in Page1.aspx.vb:

1 Dim cs As ClientScriptManager = Me.ClientScript


2 Dim script As String


3 script = String.Format("<script>window.parent.location='Page2.aspx?group={0}&type={1}';</script>", _


4 ddlGrouping.SelectedIndex, ddlStudentType.SelectedIndex)


5 cs.RegisterStartupScript(Me.GetType(), "Redirect", script)


You will notice that when the page is redirected, if you click the browser back button, you will not be able to click back since location.href will not keep the browser history. But even you use location.replace, the back button is still greyed out, I think that is because Page1.aspx is inside of an Iframe.

Reading QueryString

If Page2.aspx contains an iframe which contains Page3.aspx. And now you want to pass values from Page1.aspx to Page3.aspx through querystring, here is how:

1 <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Page2.aspx.vb" Inherits="Page2.TestIFrame" %>


2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


3 <html xmlns="http://www.w3.org/1999/xhtml" >


4 <head runat="server">


5 <title>Untitled Page</title>


6 </head>


7 <body>


8 <form id="form1" runat="server">


9 <div>


10 <iframe class=home_hero marginWidth=0 marginHeight=0


11 src="Page3.aspx?<%=Request.ServerVariables("QUERY_STRING")%>"


12 frameBorder=0 width=981 scrolling=no height=800></iframe>


13 </div>


14 </form>


15 </body>


16 </html>


If Page2.aspx contains a RadEditor which contains an iframe, the iframe contains Page3.aspx. And now you want to pass values from Page1.aspx to Page3.aspx through querystring, you cannot use the above code in the RadEditor, Request.ServerVariables("QUERY_STRING") will not be recoginized in RadEditor. You could type the following into the RadEditor:

1 <div><iframe id=test name=test marginWidth=0 marginHeight=0 frameBorder=0 width=981 scrolling=no height=800></iframe></div>


2 <script>


3 function getQueryVariable(variable)


4 {


5 var query = window.location.search.substring(1);


6 var vars = query.split("&");


7 for (var i=0;i<vars.length;i++)


8 {


9 var pair = vars[i].split("=");


10 if (pair[0] == variable)


11 {


12 return pair[1];


13 }


14 }


15 }


16 function load()


17 {


18 var g = getQueryVariable("group") ;


19 var t = getQueryVariable("type") ;


20 if (g != null && t != null)


21 {


22 document.getElementById('test').src = 'Page3.aspx?group='+g+'&type='+t;


23 }


24 }


25 window.onload = load;


26 </script>


Reference:

Javascript to parse query string variables from URL

blog comments powered by Disqus