views:

38

answers:

2

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?

A: 

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" });
Kirk Woll
I'm a bit foggy on how this would work. I don't have control over the webpage that is getting loaded into the web browser control. It sounds like the reloadFrame javascript function would need to be added to the page for this solution to work. Is this correct?
Josh
That was what I meant, yes. Thought you had control over the page with the iframe. Sorry couldn't help.
Kirk Woll
No worries, I appreciate the thoughts -- Anybody else have any ideas?
Josh
A: 

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.

Garett