Use the custom HtmlForm control in asp.net MVC to dynamically generate a form

Source: Internet
Author: User

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

Therefore, a simple HtmlForm container control is customized, which requires only a few lines of code. It seems that Asp.net is still very advantageous in encapsulating Html elements. Microsoft has defined

A large number of basic structures are 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. Just add a new control to the Controls set.

 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, the HTML code is output to the response stream in the View.

form.RendControl(Writer);

Conclusion:

Dynamic generation of HTML forms is so simple and clear. I used to splice HTML and Write it myself. Classes provided by the framework can effectively improve the opening efficiency,

Make the code readable. It is helpful to make good use of the System. Web. UI. WebControls. Table control, especially when using Table controls. I 'd like to know about PHP or other

Does the language have these useful infrastructure?

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.