Redirection from iframe in asp.net

In this tutorial we will learn how to perform redirection from IFrame in asp.net. One of my friends has got a requirement; he has a webpage that contains the form containing username and password textboxes, one asp:button to validate and verify the username and password, after performing validation, it redirects the registered user to another page but problem was that the next page was being opened inside the iframe rather than in parent window because the webpage that containing the form was also in iframe. It became quite headache for us to solve the problem but finally we did it. Let's have a look over it how to do so

Redirection from iframe in asp.net


protected void btn_Submit_Click(object sender, EventArgs e)
{
//your code to verify username/password will come here
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "if(top!=self) {top.location.href = 'next-page.aspx';}", true);
}

Note:-
In case if this code is not working properly in IE9 then you have to give the absolute path of targeting webpage such as
if(top!=self) {top.location.href = 'https://www.yourwebsite.com/next-page.aspx';}", true);
Using the server side onClick event of asp:button, we are redirecting user to another page but now page will not be open inside the iframe because the javascript code that we have written in the server side onClick event of asp:button will take care that user must be redirected to next page within parent window rather than IFrame.


So this is the way to perform redirection from iframe in asp.net.

0 comments: