
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
- Create an HTML Form Programmatically using C#
- Fill HTML Form with Submit Button in C#
- Free API License
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:
- Initialize an instance of HTMLDocument class.
- Create new HTML form with CreateNew method.
- Input simple text type field.
- Create a radio button input element.
- Input submit button in HTML form and add current nodes.
- 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:

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:
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:
Source Code
Below is the complete source code to fill HTML forms programmatically using C#:
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.