Example of an ASP. Net method code that dynamically generates an HTML form

Source: Internet
Author: User
after testing System.Web.UI.HtmlControls under the HtmlForm Class, which is the form form object that we use in traditional ASP., is not suitable for dynamically generating HTML code.

A simple HtmlForm container control is customized, requiring only a few lines of code. It seems that ASP is still very advantageous in encapsulating HTML elements, and Microsoft has defined a lot of infrastructure for us, and it is easy to extend the use.

public class myhtmlform:htmlcontainercontrol{public    myhtmlform (): Base ("form")    {this      . Attributes.Add ("Method", "post");    }    public string Action    {      set      {        Attributes.Add ("action", value);    }}}

It is easy to use, direct new, and then add the control 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);

Conclusion:

The dynamic generation of HTML forms is so straightforward. I used to splice html myself and write. Being good at using the classes provided by the framework can effectively improve open efficiency and make the code more readable. Especially when making a table control, System.Web.UI.WebControls.Table it can be helpful to use the control.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.