Monday, November 19, 2007

How to redirect parent iframe from child iframe

How to redirect parent iframe from child iframe
 
For example while working on child page (iFrame) if session timeout then error page should refect in complate application not only child page (iFrame), to achive this please see following solutions
 
Solution-1
 
On click of child iFrame page control write following lines of code in code behind
 
Response.Write("<script>window.open('Error.aspx','_parent');</script>");
 
Solution-2
 
Write java script function in child page (iFrame) to submit parent page and set some hidden variable Now in the parent page in page load events according to hidden variable value, redirect to error page.
 
Code in child page
 
Java script function in aspx file
 
//when the session is timeout, its redirected to the home page
function RedirectError(errorMsg)
{           
window.parent.document.getElementById('hdnErrorMsg').value=errorMsg;
window.parent.document.forms.frmHome.submit();
}
 
Note: "frmHome" is parent page and "hdnErrorMsg" hidden variable declared in child page
 
Call this java script function from child page code behind
 
C# code in code behind
 
try
{
      // Code logic
}
catch (System.Security.SecurityException ex)
{
StringBuilder activateHide = new StringBuilder();
activateHide.Append("<script language='javascript'>");                activateHide.Append("RedirectError('SessionTimeout');");
activateHide.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "Contents", activateHide.ToString());
}
 
Code in Parent page
 
In the parent page (Home.aspx) code bahind page_load event handle this in if..else loop
protected void Page_Load(object sender, EventArgs e)
{
if (hdnErrorMsg.Value != "")
{
Response.Redirect("Error.aspx?ErrorMsg=" + hdnErrorMsg.Value);
}
else
{
      // Code logic
}
}
 


Never miss a thing. Make Yahoo your homepage.