Create Fill HTML Form Submit C#

HTML forms are frequently used for getting input from the users. You can fill in different form fields containing Checkbox, Text, Radio, and Submit button for input. You can create or fill-in the HTML form programmatically using the C# language. This article covers the following sections related to HTML forms:

Create or Fill HTML Form - C# API Installation

Aspose.HTML for .NET API lets you create or fill HTML forms with Submit buttons programmatically using C# language. You can configure the API by downloading a DLL file from the Downloads section, or quickly install it with the following NuGet installation command:

PM> Install-Package Aspose.Html

Create an HTML Form Programmatically using C#

You can create a HTML form programmatically using C# language. Please follow the steps below for creating it from scratch:

  1. Initialize an instance of HTMLDocument class.
  2. Create new HTML form with CreateNew method.
  3. Input simple text type field.
  4. Create a radio button input element.
  5. Input submit button in HTML form and add current nodes.
  6. Save HTML form.

The following code shows how to create a HTML form programmatically using C#:

// Initialize an instance of HTMLDocument class
using (HTMLDocument document = new HTMLDocument())
{
// Create new HTML form with CreateNew() method
using (var editor = FormEditor.CreateNew(document))
{
// Input simple text type field
var name = editor.AddInput("name");
name.Type = InputElementType.Text;
// Input a radio button
var size = editor.AddInput("size");
size.Type = InputElementType.Radio;
size.HtmlElement.InnerHTML = "Sample Text";
// Input submit button in HTML form
var button = editor.Add<Forms.ButtonElement>("btn");
button.Value = "submit";
button.HtmlElement.InnerHTML = "Sample Button";
// Adds the current node
document.Body.AppendChild(editor.Form);
}
// Save HTML form
document.Save("Create-HTML-Form.html");
}

Moreover, below is another approach for creating HTML form using native HTML form element in C#:

// Create an HTML Form by using native HTML API
using (var document = new Aspose.Html.HTMLDocument())
{
var editor = Aspose.Html.Forms.FormEditor.CreateNew(document);
var body = document.Body;
// create form-element
var form = (HTMLFormElement)document.CreateElement("form");
// form.Action = "action_page.php";
// Create FirstName label
var label = (HTMLLabelElement)document.CreateElement("label");
label.TextContent = "First name:";
label.For = "fname";
form.AppendChild(label);
form.AppendChild(document.CreateElement("br"));
// Create FirstName field
var input = (HTMLInputElement)document.CreateElement("input");
input.Type = "text";
input.Id = "fname";
input.Name = "fname";
form.AppendChild(input);
form.AppendChild(document.CreateElement("br"));
form.AppendChild(document.CreateElement("br"));
// Create LastName label
label = (HTMLLabelElement)document.CreateElement("label");
label.TextContent = "Last name:";
label.For = "lname";
form.AppendChild(label);
form.AppendChild(document.CreateElement("br"));
// Create LastName field
input = (HTMLInputElement)document.CreateElement("input");
input.Type = "text";
input.Id = "lname";
input.Name = "lname";
form.AppendChild(input);
form.AppendChild(document.CreateElement("br"));
form.AppendChild(document.CreateElement("br"));
// Create RadioButton field
input = (HTMLInputElement)document.CreateElement("input");
input.Type = "radio";
input.Id = "radio1";
input.Name = "Topping";
form.AppendChild(input);
// Create RadioButton label
label = (HTMLLabelElement)document.CreateElement("label");
label.TextContent = "Male";
label.For = "radio1";
form.AppendChild(label);
form.AppendChild(document.CreateElement("br"));
form.AppendChild(document.CreateElement("br"));
// Create RadioButton field
input = (HTMLInputElement)document.CreateElement("input");
input.Type = "radio";
input.Id = "radio2";
input.Name = "Topping";
form.AppendChild(input);
// Create RadioButton label
label = (HTMLLabelElement)document.CreateElement("label");
label.TextContent = "Female";
label.For = "radio2";
form.AppendChild(label);
form.AppendChild(document.CreateElement("br"));
form.AppendChild(document.CreateElement("br"));
// Create Submit button
input = (HTMLInputElement)document.CreateElement("input");
input.Type = "submit";
input.Value = "Submit";
form.AppendChild(input);
body.AppendChild(form);
document.Save(dataDir + "Create-HTML-Form.html");
}

The following screenshot shows the output HTML form created with above code snippet:

Create HTML Form Submit Btn

Fill HTML Form with Submit Button in C#

You have already explored how to create an HTML form. Now you can learn how to fill HTML form and send the data to any remote server directly from your C# application. You can iterate through different items in the form and then use Submit button for sending the data. Here you will be working with a template form created by httpbin.org. It includes filling Text, Radio buttons, as well as Checkboxes that you be learning below:

Select the Value in Input Type Checkbox

Checkboxes are helpful where you need to choose more than one option. You can easily select the value in input type checkbox with the following code:

elements = editor
.Where(x => x.ElementType == Forms.FormElementType.Input)
.Cast<Aspose.Html.Forms.InputElement>()
.Where(x => x.Type == Forms.InputElementType.Checkbox);
foreach (Forms.InputElement input in elements)
{
// Choose onion and cheese both options in Check boxes
if (input.Value == "onion" || input.Value == "cheese")
{
input.SetCheckboxValue(true);
}
}

Fill the Input Type Text and Radio Field

An HTML form may contain several text boxes and radio button fields for collecting data. You can fill the input text with the following code:

// You can fill the data up using direct access to the input elements, like this:
editor["custname"].Value = "Aspose";
// or this:
var comments = editor.GetElement<Aspose.Html.Forms.TextAreaElement>("comments");
comments.Value = "Extra Ketchup PLEASE!";
// or even by performing a bulk operation, like this one:
editor.Fill(new Dictionary<string, string>()
{
{"custemail", "test@test.com"},
{"custtel", "+123-456-789"}
});

Likewise, you can select radio button fields with the code below:

var elements = editor
.Where(x => x.ElementType == Forms.FormElementType.Input)
.Cast<Aspose.Html.Forms.InputElement>()
.Where(x => x.Type == Forms.InputElementType.Radio);
foreach (Forms.InputElement input in elements)
{
// Select medium as size in the Radio button
if (input.Value == "medium")
{
input.SetRadioValue(true);
}
}

Sending Data with Submit Button in HTML

You can send filled data in HTML form with the Submit button using FormSubmitter class. The following code shows how to call Submit() method in C# code:

// Create an instance of form submitter
var submitter = new Aspose.Html.Forms.FormSubmitter(editor);
// Submit the form data to the remote server.
// If you need you can specify user-credentials and timeout for the request, etc. with overload methods
var result = submitter.Submit();

Source Code

Below is the complete source code to fill HTML forms programmatically using C#:

// Initialize an instance of HTML document from 'https://httpbin.org/forms/post' url
using (var document = new HTMLDocument(@"https://httpbin.org/forms/post"))
{
// Create an instance of Form Editor
using (var editor = FormEditor.Create(document, 0))
{
var elements = editor
.Where(x => x.ElementType == Forms.FormElementType.Input)
.Cast<Aspose.Html.Forms.InputElement>()
.Where(x => x.Type == Forms.InputElementType.Radio);
foreach (Forms.InputElement input in elements)
{
// Select medium as size in the Radio button
if (input.Value == "medium")
{
input.SetRadioValue(true);
}
}
elements = editor
.Where(x => x.ElementType == Forms.FormElementType.Input)
.Cast<Aspose.Html.Forms.InputElement>()
.Where(x => x.Type == Forms.InputElementType.Checkbox);
foreach (Forms.InputElement input in elements)
{
// Choose onion and cheese both options in Check boxes
if (input.Value == "onion" || input.Value == "cheese")
{
input.SetCheckboxValue(true);
}
}
// You can fill the data up using direct access to the input elements, like this:
editor["custname"].Value = "Aspose";
// or this:
var comments = editor.GetElement<Aspose.Html.Forms.TextAreaElement>("comments");
comments.Value = "Extra Ketchup PLEASE!";
// or even by performing a bulk operation, like this one:
editor.Fill(new Dictionary<string, string>()
{
{"custemail", "test@test.com"},
{"custtel", "+123-456-789"}
});
// Create an instance of form submitter
using (var submitter = new Aspose.Html.Forms.FormSubmitter(editor))
{
// Submit the form data to the remote server.
// If you need you can specify user-credentials and timeout for the request, etc.
var result = submitter.Submit();
// Check the status of the result object
if (result.IsSuccess)
{
// Check whether the result is in json format
if (result.ResponseMessage.Headers.ContentType.MediaType == "application/json")
{
// Dump result data to the console
Console.WriteLine(result.Content.ReadAsString());
}
else
{
// Load the result data as an HTML document
using (var resultDocument = result.LoadDocument())
{
// Inspect HTML document here.
}
}
}
}
}
}

Free API License

You can evaluate Aspose.HTML for .NET API without any Evaluation Limitations by requesting a Free Temporary License.

Conclusion

In conclusion, you have learned how to create an HTML form from scratch, as well as how to fill an existing HTML form and sending the data to a remote server from your C# application. You can also visit the Documentation section to explore several other features. Please feel free to contact us at the Free Support Forum for any of your queries.

See Also