Home Backend Development C#.Net Tutorial Code example of asp.net method to dynamically generate HTML form

Code example of asp.net method to dynamically generate HTML form

Apr 25, 2017 pm 02:03 PM
asp.net html form Dynamically generated

After testingThe HtmlForm class under System.Web.UI.HtmlControls is what we use in the traditional The Form form object used in asp.net is not suitable for dynamically generating Html code.

So I customized a simple HtmlForm container control, which only requires a few lines of code. It seems that Asp.net still has great advantages in encapsulating Html elements. Microsoft has defined a large number of basic structures for us, which is easy to expand and use.

public class myHtmlForm:HtmlContainerControl
{
    public myHtmlForm(): base("form")
    {
      this.Attributes.Add("method", "post");
    }
    public string Action
    {
      set
      {
        Attributes.Add("action", value);
      }
    }
}
Copy after login

It is very simple to use, just use new, and then add controls to the Controls collection.

myHtmlForm form = new myHtmlForm();
form.ID = "myform";
form.Action = "test.aspx";
HtmlInputHidden hidf= new HtmlInputHidden();
hidf.ID = hidf.Name = "hidden";
form.Controls.Add(hidf);
Copy after login

Finally, in View, output the HTML code to the response stream.

form.RendControl(Writer);
Copy after login

Conclusion:

Dynamic generation of HTML forms is so simple and clear. I used to splice HTML myself and then write it. Being good at using the classes provided by the framework can effectively improve the opening efficiency and make the code more readable. Especially when making table controls, it will be helpful to make good use of the System.Web.UI.WebControls.Table control.

The above is the detailed content of Code example of asp.net method to dynamically generate HTML form. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to use PHP to dynamically generate QR codes How to use PHP to dynamically generate QR codes Sep 05, 2023 pm 05:45 PM

How to use PHP to dynamically generate QR codes. QR codes (QRCode) are widely used in various fields. They can store a large amount of information and are easy to scan. In web applications, we often need to dynamically generate QR codes to provide users with convenient operations. This article will introduce how to use PHP to dynamically generate QR codes. 1. Install and configure the PHPQRCode library. In order to facilitate the generation of QR codes, we can use the PHPQRCode library. First, we need

How to use JavaScript to dynamically generate tables? How to use JavaScript to dynamically generate tables? Oct 24, 2023 am 10:34 AM

How to use JavaScript to dynamically generate tables? In web development, tables are often used to display data or create forms for data entry. Using JavaScript can realize the function of dynamically generating tables, so that the table contents can be dynamically updated according to changes in data. This article will introduce in detail how to use JavaScript to dynamically generate tables through specific code examples. 1. HTML structure preparation First, prepare a container in HTML

How to get HTML form data as text and send to html2pdf? How to get HTML form data as text and send to html2pdf? Sep 06, 2023 pm 12:21 PM

html2pdf is a JavaScript package that allows developers to convert html to canvas, pdf, images, and more. It takes html as parameter and adds it to pdf or desired document. Additionally, it allows users to download the document after adding html content. Here we will access the form and add it to the pdf using the html2pdfnpm package. We will see different examples to add form data to pdf. Syntax User can follow the following syntax to pass html form data as text and send it to html2pdf. varelement=document.getElementById('form');html2

How to allow multiple file uploads in HTML form How to allow multiple file uploads in HTML form Aug 28, 2023 pm 08:25 PM

In this article, we will learn how to allow multiple files uploads in HTML forms. We use multiple attributes to allow multiple file uploads in HTML forms. Several properties are available for email and file input types. Ifyouwanttoallowausertouploadthefiletoyourwebsite,youneedtouseafileuploadbox,alsoknownasafile,selectbox.Thisiscreatedusingthe<in

PHP file upload tutorial: How to upload files using HTML forms PHP file upload tutorial: How to upload files using HTML forms Jun 11, 2023 am 08:10 AM

PHP file upload tutorial: How to use HTML forms to upload files In the process of website development, the file upload function is a very common requirement. As a popular server scripting language, PHP can implement the file upload function very well. This article will introduce in detail how to use HTML forms to complete file uploads. 1. HTML form First, we need to use an HTML form to create a file upload page. In the HTML form, the enctype attribute needs to be set to "multipart/form-

Use jQuery to generate dynamic tables with row numbers Use jQuery to generate dynamic tables with row numbers Feb 26, 2024 pm 09:39 PM

jQuery Tips: Dynamically Generate Tables and Automatically Add Row Numbers In web development, it is often necessary to dynamically generate tables to display data. At the same time, in order to make it easier for users to view the table content, we often need to add row numbers to the table. This article will introduce how to use jQuery to dynamically generate tables and automatically increase row numbers. First, we need a simple HTML structure, including a button to trigger the dynamic generation of the table, and an empty element to place the generated table

How to use template functions in Go language to dynamically generate Word documents? How to use template functions in Go language to dynamically generate Word documents? Jul 31, 2023 pm 09:21 PM

How to use template functions in Go language to dynamically generate Word documents? With the advent of the information age, dynamically generating Word documents has become a common need for companies and individuals to process documents. As an efficient and concise programming language, Go language has built-in template functions that can help us quickly realize the function of dynamically generating Word documents. This article will introduce how to use template functions in the Go language to dynamically generate Word documents and provide relevant code examples. 1. Preparation Before starting, we need to

How to implement dynamically generated web navigation menu using PHP and XML How to implement dynamically generated web navigation menu using PHP and XML Jul 29, 2023 am 09:00 AM

Overview of how to use PHP and XML to implement dynamically generated web page navigation menu: In web design and development, the navigation menu is a very important component, which can help users quickly locate various pages of the website and provide convenient navigation functions. This article will introduce how to use PHP and XML to implement dynamically generated web navigation menus, making the website navigation menu more flexible and easier to maintain. Step 1: Create an XML file First, we need to create an XML file to store the navigation menu data of the website. Can use any

See all articles