You can read all the details at: Create or Fill HTML Form with Submit Button using C#
Last active
December 23, 2021 07:12
-
-
Save aspose-com-gists/d5cbfdbf27a91d2c85ba2db154909d12 to your computer and use it in GitHub Desktop.
Create Fill HTML Form Submit Button | Input Type Checkbox Text Radio Btn Submit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment