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.