Asp.Net Server Side: How to get Iframe’s Parent Window Url?

In Asp.Net server sided button’s click event, I somehow needed to find parent window url of an iframe, which means the url you see in the address bar of your web browser.

Let’s think of 2 pages: Parent.aspx and Child.aspx. Child.aspx is loading to an iframe in Parent.aspx. If you have a button in Child.aspx, you will be getting “Child.aspx” result when you use Request.Url method in this button’s click event.

But you see “Parent.aspx” in the address bar, right? So how to get it?

Here you need to work with client side peacefully. Otherwise the server will keep giving you the same result.

First, place a button running at server, add a click event and a client-click event.

    <asp:Button ID="btnFindParentUrl" runat="server" Text="Get Url!" OnClick="btnFindParentUrl_Click" OnClientClick="fillHidden();"></asp:Button>

Put a hidden textbox inside a div with style=”display: none;” (not Visible=”false” for button because if you do client can’t see and fill it):

    <div style="display: none;">
        <asp:TextBox ID="txtHiddenUrlField" runat="server" BorderStyle="None" Font-Size="0px" ForeColor="#F6F6F6" Height="0px" Width="0px"></asp:TextBox>

Now place javascript code of fillHidden() function:

    <script type="text/javascript">
        function fillHidden() {
            document.getElementById('<%= txtHiddenUrlField.ClientID %>').value = parent.document.location.href;

That’s all you have to do at client side.
Let’s go to the server:

    protected void btnFindParentUrl_Click(object sender, EventArgs e)
        string parentUrl = txtHiddenUrlField.Text;

This way, you should be getting parent url from button in an iframe.


Add a Link Button to Navigate Previous Page

In my Asp.Net web page, I had a trouble with navigating to the previous URL. I guess possibly because of postback, my linkbutton was navigating to the current page when I use this method:


I was unable to give previous page’s URL as a link simply, because there were a lot of data so it always was taking a while when I “navigated to the last page” with this method.

Then I saw this great JavaScript method and it works perfectly so far. All you have to do is to insert a Asp:LinkButton then add this to your Page_Load event:

lnkBtnPreviousPage.Attributes.Add("onClick", "javascript:history.back(); return false;");
Post a comment

Asp.Net Menu | Hide The Last Seperator Image

Sometimes it’s hard to understand why things work different in Asp.Net. Maybe that’s because I’m not so expert with it. Though, it makes no sense to me not to be able to seperate Asp.Net Menu items with chars like ‘|’ or ‘-‘ etc. Allright, we create little seperator images but when we do it, it either adds before all elements or after. Like “Home | News | Contact |”. What’s the sense of last seperator? What is it seperating actually? I never seen such usage…

Anyway, there’s always a way to do what we want but I just don’t know why we have to cross that bridge.

Here’s the code that hides last menu item’s seperator image so that we have a normal menu:

protected void myMenu_PreRender(object sender, EventArgs e)
    //We define seperator image for all menu items except the last (Items.Count -1)
    for (int i = 0; i < myMenu.Items.Count-1; i++)
        myMenu.Items[i].SeparatorImageUrl = "~/Images/MasterPage/menu-seperator.png";
Post a comment