{{ figure align=center src=“images/Create-Fill-HTML-Form-Submit.png” alt=“Create Fill HTML Form Submit C#” }}

HTML forms are used to collect user input. You can fill fields such as Checkbox, Text, Radio, and Submit button. With C#, you can create or fill an HTML form programmatically. 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#:

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Create-HTML-Form.cs” }}

Another approach uses native HTML form elements in C#:

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Create-HTML-Form-Native.cs” }}

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

{{ figure align=center src=“images/Create-HTML-Form-Submit-Btn.png” alt=“Create HTML Form Submit Btn” }}

Fill HTML Form with Submit Button in C#

You have learned how to create an HTML form. Now you can fill an existing form and send the data to any remote server directly from your C# application. Iterate through form items and use the Submit button to send data. This example uses a template form from httpbin.org and demonstrates filling Text, Radio buttons, and Checkboxes:

Select the Value in Input Type Checkbox

Checkboxes allow multiple selections. Select a checkbox value with the following code:

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Fill-Checkbox.cs” }}

Fill the Input Type Text and Radio Field

An HTML form may contain several text boxes and radio button fields. Fill a text input with this code:

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Fill-Text-Field.cs” }}

Select a radio button with the code below:

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Fill-Radio-Button.cs” }}

Sending Data with Submit Button in HTML

Send the filled data using the FormSubmitter class. Call the Submit() method as shown:

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Submit-Button-HTML-Form.cs” }}

Source Code

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

{{ gist aspose-com-gists d5cbfdbf27a91d2c85ba2db154909d12 “Fill-HTML-Form-Submit-Button.cs” }}

Free API License

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

Conclusion

You have learned how to create an HTML form from scratch, fill an existing form, and send the data to a remote server using C#. Visit the Documentation for more features, or ask questions on the Free Support Forum.

See Also