Josh
Josh

Reputation: 73

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?

Upvotes: 1

Views: 3903

Answers (2)

Garett
Garett

Reputation: 16828

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.

Upvotes: 1

Kirk Woll
Kirk Woll

Reputation: 77546

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" });

Upvotes: 2

Related Questions