简体   繁体   English

在Web浏览器控件中刷新iFrame

[英]Refreshing an iFrame in a Web Browser Control

C# Visual Studio 2010 C#Visual Studio 2010

I have a complex webpage that contains several iframes that I am loading into a web browser control. 我有一个复杂的网页,其中包含要加载到网络浏览器控件中的多个iframe。 I'm trying to figure out a way to refresh one of the iframes when a user clicks a button on the windows form. 当用户单击Windows窗体上的按钮时,我试图找出一种刷新iframe的方法。

I can't find anything specific to refreshing a single iframe. 我找不到特定于刷新单个iframe的内容。 Any ideas? 有任何想法吗?

From within the DOM, you can just invoke: 在DOM中,您可以调用:

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

Using the WebBrowser control, you can execute javascript yourself, by using the InvokeScript method of Document: 使用WebBrowser控件,您可以使用Document的InvokeScript方法自己执行javascript:

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: 并在您的C#代码中调用此代码:

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

How about using MSHTML and the reload method of the IHTMLLocation interface. 如何使用MSHTMLIHTMLLocation接口的重载方法。 You would add a reference to Microsoft.mshtml then try: 您将添加对Microsoft.mshtml的引用,然后尝试:

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. 值为true将从服务器重新加载页面,而false则从缓存中检索页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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