Search code examples
c#asp.netformsrunat

ASP.NET Website: page with multiple forms


I'm having a problem while creating dynamically a web page in ASP.NET (C#).

I need to insert multiple form tags in that page, I know that I can't put more than one with runat="server" attribute and I don't need it.

I want to place them from C# (one for each element I've got to manage), without the runat attribute but the HtmlForm object that I use for insert the form adds the runat attribute automatically and I can't remove it (tried with form.Attributes.Remove("runat"))

If I use a simple string like:

"<form id="someID" method="POST" action=""></form>"

and I add it multiple times into my div it works. The point is that I don't want to insert ALL my HTML objects writing them in a string and add it with InnerHTML method. I'm looking for an object that manage a Form without the runat attribute or a way to remove that from HtmlForm.


Solution

  • A user control is probably the cleanest solution but you can add an HtmlGenericControl instead of a HtmlForm object which isn't bound to any specific attributes.

    Dim ctrl As New HtmlGenericControl("form")
    ctrl.Attributes.Add("id", "someID")
    ctrl.Attributes.Add("method", "POST")
    ctrl.Attributes.Add("action", "")
    OuterDivContainer.Controls.Add(ctrl)