Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Fill HTML Form Submit Button | Input Type Checkbox Text Radio Btn Submit
// 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");
}
// 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");
}
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);
}
}
// 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.
}
}
}
}
}
}
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);
}
}
// 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
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment