0

I have a simple Window Forms application that opens up a webpage with set parameters.

The link send the user to a page with 2 text box fields and a submit button.

I am trying to automate this process so it grabs the parameter values and puts it into the text box then clicks submit .

This is my current code for my windows form:

using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

    public Form1() {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {
        libLink.Links.Remove(libLink.Links[0]);
        libLink.Links.Add(0, libLink.Text.Length,
            "http://www.example.com/?UserName=value1&FirstName=value2");
    }

    private void libLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
        ProcessStartInfo sInfo = new ProcessStartInfo(e.Link.LinkData.ToString());
        Process.Start(sInfo);
    }
}
}

How can I create a script that that takes those parameters in the URL to populate two text box fields and then submit the form?

This is my HTML Page:

  <form action="/send" method="post" novalidate="novalidate">
      <input class="form-control" data-val="true" data-val-UserName="Wrong username" data-val-required="Enter a valid Username" id="Username" name="Username" placeholder="Username" type="text" value="">
      <input class="form-control" id="FirstName" name="FirstName" placeholder="First Name" type="text" value="">   
      <button type="submit">Sign In</button>
  </form>

Fairly new to coding so I tried to keep my code as simple as possible.

I looked at possible methods such as QueryStrings, JavaScript and Jquery, but I am not sure how to approach this problem.

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34

1 Answers1

-1

There's a few ways of doing it really but i'll give you a basic walk through of how i would do it in javascript with a bit of JQuery.

we have variable with the URL we start by:

var url = "http://www.example.com/?UserName=value1&FirstName=value2"
var params_string = url.split("?")[1] //UserName=value1&FirstName=value2

so first we split the string into a list like above which returns a list of the items which are separated by the "?" character, but we only need the second item(at index 1) so we add the [1] to the end to only store that bit.

We then split this again to get the individual parameters.

var params_string_list = params_string.split("&")
["UserName=value1","FirstName=value2"]

which returns a list as above, again need to break that down i would make it into an object like so :

var params = {}
for(var i =0;i < params_string_list.length;i++ ){
     var temp = params_string_list[i].split("=") // looks like ["UserName","value1"]
     params[temp[0]]= temp[1]
} //params now looks like  {"UserName":"value1","FirstName":"value2"}

as this makes it easy to access and use. we can then do the following to set the values in the form:

if(params.UserName){
    $('#Username').val( param.UserName );
}
if(params.FirstName){
    $('#FirstName').val( param.FirstName );
}

if statments are there to check that the value exists in the object so we don't sent the value to "undefined" by accident.

Hope this helps.

  • Thank you for your input, I am going to test this out. From the way you explained it, it really clears things up. How would you go about submitting the form after the field are inputted – Danny Nguyen Mar 24 '17 at 15:47
  • I am little confused. OP says its windows form application, but the accepted answer is using jQuery – LP13 Mar 24 '17 at 16:31
  • @LP13 I am new to using stack overflow I wanted to upvote his comment, Sorry for the confusion lol. – Danny Nguyen Mar 24 '17 at 17:02
  • It looks like the original poster is asking about how he can programatically fill out and submit a form on a webpage from within a windows forms app, presumably using a WebBrowser control or something. As such, what he needs is probably advice on using the WebBrowser control that is acting as his client, rather than javascript/jquery techniques. Apples and oranges. – Joe Irby Mar 24 '17 at 20:14