How to access a parent element from iframe using c#?

by elise_daugherty , in category: Third Party Scripts , 8 months ago

How to access a parent element from iframe using c#?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 8 months ago

@elise_daugherty 

To access the parent element from an iframe in C#, you can use JavaScript to communicate between the iframe and parent window. Here is a step-by-step guide on how to achieve this:

  1. Add a JavaScript function in the parent window that can be called from the iframe:
1
2
3
function getParentElement() {
   return document.getElementById('parentElementId');
}


  1. Add an iframe to the parent window and set an ID attribute for the iframe:
1
<iframe id="myIframe" src="https://www.example.com"></iframe>


  1. In the iframe document, call the parent window function to access the parent element:
1
2
3
4
5
6
7
using System;
using System.Web.UI;

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "GetParentElement", "var parentElement = window.parent.getParentElement();", true);
}


  1. You can now access the parent element using the parentElement variable in the iframe document.


Please note that this method relies on JavaScript to access the parent element from the iframe. You cannot directly access the parent elements in C# code without using JavaScript.

Related Threads:

How to download iframe video with c#?
How to pass argument from iframe to parent?
How to pass a value from iframe to the parent?
How to get parent iframe id in javascript?
How to resize parent window size from iframe?
How to refresh iframe when parent page is refreshed?