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

Y2J
Release: 2017-04-25 14:03:16
Original
1771 people have browsed it

After testingTheHtmlFormclass under System.Web.UI.HtmlControlsis 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 simpleHtmlFormcontainer 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 theSystem.Web.UI.WebControls.Tablecontrol.

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!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!