Auto Ad Code

Wednesday, August 11, 2010

Redirecting Parent page from iframe using same query string

Once I got caught in a situation that I had to check Database changes from a hidden iframe placed withing a page and then had to redirect the parent page in a way that parent window don't lose its querystring.
Following was the solution which I used.
========================================
In my main page(testpage.aspx) I had following statement for hidden iframe

<iframe src="refreshpage.aspx?<%= Request.QueryString.ToString %>" style="display: none;"></iframe>


in refreshpage.aspx I was having nothing but a timer and its tick event which was having the code to check in Database that either some new entry has been made or not. And in case there was any new entry I was refreshing the parent page by using Javascript.
Following is the HTML code for the timer part of my refreshpage.aspx.

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="5000"></asp:Timer>

Remember that scriptmanager is must as I am using Timer of AJAX Extensions.

Following is the VB code for refreshpage.aspx.

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If RecordInserted() Then
'RecordInserted is my own function to check if data is enetered or not.
ClientScript.RegisterClientScriptBlock(Me.GetType, "", "<script>window.parent.location.href = ""testpage.aspx?" & Request.QueryString.ToString & """;</script>")
End If
End Sub
====================================

This is it for the code and following is the explanation of above.
refreshpage.aspx checks the database after every 5 seconds that if there is new entry or not and if there is any it refreshes the parent page using Javascript.
<%= Request.QueryString.ToString %> is required in case when QueryString is required for the redirect from iframe.

Hope this will help you :)

No comments:

Post a Comment