Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to generate HTML form dynamically by asp.net

Shulou Source: shulou.com Published: 2022-05-31 13:13:28 09月15日 Update

This article mainly introduces "how asp.net dynamically generates HTML forms". In daily operation, I believe many people have doubts about how asp.net dynamically generates HTML forms. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "how to generate HTML forms dynamically by asp.net". Next, please follow the editor to study!

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

So I customize a simple HtmlForm container control that requires only a few lines of code. It seems that Asp.net has an advantage in encapsulating Html elements. Microsoft defines a lot of infrastructure 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);}

It's easy to use, new directly, 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)

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

Form.RendControl (Writer); at this point, the study on "how asp.net dynamically generates HTML forms" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Dynamic forms generation learning code controls more help utility and then that is advantages traditions elements foundations containers objects articles methods aspects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker Redmi Huawei Microsoft