Irfan TahirKheli
Irfan TahirKheli

Reputation: 3662

Web browser control open up the next page in IE on invoking form submit method

I am trying to create auto form submit application. I need to subscribe on different sites. It involves submitting two forms. I am using .Net web browser control. When i fill the first form and call submit method of the form or click method of the button, both opens up the next page in IE. ( In window browser the first form opens the next form in new tab on hitting submit button)

HtmlElementCollection inputs = form.GetElementsByTagName("input");
foreach (HtmlElement input in inputs)
{
       if (input.GetAttribute("type") == "text")
       {
            inputname = input.GetAttribute("name");
            input.SetAttribute("value", email);
       }

        else if (input.GetAttribute("type") == "submit")
        {
             //input.InvokeMember("click");
             form.InvokeMember("submit");
             break;
         }
 }

So the issue is it does't open the next form in the control rather opening it up in IE. How can i make it so it opens the next form within the control.

Any help would be appreciated. Thanks

Upvotes: 2

Views: 1888

Answers (1)

noseratio
noseratio

Reputation: 61726

The following works for me. Try it as is and find what's different from your code.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformsWb
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        const string HTML_DATA =
            "<form target='_blank' action='http://requestb.in/yc89ojyc' method='post' enctype='multipart/form-data'>" +
            "<input type='hidden' name='arg' value='val'>" +
            "<button type='submit' name='go'  value='submit'>Submit</button>" +
            "</form>";

        // this code depends on SHDocVw.dll COM interop assembly,
        // generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
        // and add as a reference to the project

        private async void MainForm_Load(object sender, EventArgs e)
        {
            // make sure the ActiveX has been created
            if ((this.webBrowser.Document == null && this.webBrowser.ActiveXInstance == null))
                throw new ApplicationException("webBrowser");

            // handle NewWindow
            var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;
            activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
            {
                Processed = true;

                object flags = Flags;
                object targetFrame = Type.Missing;
                object postData = PostData != null ? PostData : Type.Missing;
                object headers = !String.IsNullOrEmpty(Headers) ? Headers.ToString() : Type.Missing;

                SynchronizationContext.Current.Post(delegate
                {
                    activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
                }, null);
            };

            // navigate to a stream
            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(HTML_DATA);
                writer.Flush();
                stream.Position = 0;

                // naviage and await DocumentCompleted
                var tcs = new TaskCompletionSource<bool>();
                WebBrowserDocumentCompletedEventHandler handler = (s, arg) =>
                    tcs.TrySetResult(true);
                this.webBrowser.DocumentCompleted += handler;
                this.webBrowser.DocumentStream = stream;
                await tcs.Task;
                this.webBrowser.DocumentCompleted -= handler;
            }

            // click the button
            var button = this.webBrowser.Document.GetElementById("go");
            button.InvokeMember("click");
        }
    }
}

Upvotes: 2

Related Questions