简体   繁体   中英

Refreshing an iFrame in a Web Browser Control

C# Visual Studio 2010

I have a complex webpage that contains several iframes that I am loading into a web browser control. I'm trying to figure out a way to refresh one of the iframes when a user clicks a button on the windows form.

I can't find anything specific to refreshing a single iframe. Any ideas?

From within the DOM, you can just invoke:

document.getElementById([FrameID]).contentDocument.location.reload(true);

Using the WebBrowser control, you can execute javascript yourself, by using the InvokeScript method of Document:

browser.Document.InvokeScript([FunctionName], [Parameters]);

Put these two concepts together by writing your own function in the parent page:

function reloadFrame(frameId) {
    document.getElementById(frameId).contentDocument.location.reload(true);
}

And invoke this in your C# code:

browser.Document.InvokeScript("reloadFrame", new[] { "myFrameId" });

How about using MSHTML and the reload method of the IHTMLLocation interface. You would add a reference to Microsoft.mshtml then try:

IHTMLDocument2 doc = webBrowser1.Document.Window.Frames["MyIFrame"].Document.DomDocument as IHTMLDocument2;
IHTMLLocation location = doc.location as IHTMLLocation;

if (location != null)
    location.reload(true);

A value of true reloads the page from the server, while false retrieves it from the cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM