Search code examples
c#htmlhtmltextwriter

How to generate Form's OnSubmit attribute with HtmlTextWriter


I need the Html will be something like:

<form onsubmit="return manipulateForm()">
...
</form>

I'm generating it with

HtmlTextWriterAttribute key;
string value;

using (HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter(innerBuffer)))
{
    writer.AddAttribute(key, value);
}

The problem is that the HtmlTextWriterAttribute Enum does not have a definition for OnSubmit, hot do i get passed it?


Solution

  • Just saw that the class of HtmlTextWriter has override function for AddAttribute

    public virtual void AddAttribute(string name, string value);

    So the solution will be like:

    using (HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter(innerBuffer)))
    {
        writer.AddAttribute("onsubmit", "return manipulateForm()");
    }