System.Windows.Forms.WebBrowser open links in same window or new window with same session

前端 未结 3 1328
有刺的猬
有刺的猬 2020-12-03 05:42

When using the .NET WebBrowser control how do you open a link in a new window using the the same session (ie.. do not start a new ASP.NET session on the server), or how do y

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 06:14

    Slightly cleaned up version of Greg's answer. It modifies the passed-in control's behavior rather than relying on a global variable. Usage:

    InlinePopups(webBrowser1);
    

    Code:

    // interface to expose ActiveX methods
    private SHDocVw.WebBrowser_V1 Web_V1;
    private void InlinePopups(WebBrowser browser)
    {
        // hooks to force new windows to open in the current instance
        Web_V1 = (SHDocVw.WebBrowser_V1)browser.ActiveXInstance;
        Web_V1.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler((string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) =>
        {
            Processed = true; // stop event from being processed
    
            // open in the existing window
            browser.Navigate(URL);
        });
    }
    

    Still needs the reference to %WINDIR%\system32\shdocvw.dll, of course.

提交回复
热议问题