How can I set a Button's type to "Button" (as opposed to the default "Submit") in C#?

1.2k views Asked by At

According to an answer here, I can prevent a button from submitting the form by setting its type value to "Button", like so in HTML:

<button type="button">Cancel changes</button>

...but how can I do this in C#? I am creating my controls dynamically, something like this (pseudocode, as I'm away from my IDE):

button btn = new Button 
             {
               CSS = "bla"
             }
btn.Type = "Button"; // <- something like this?
1

There are 1 answers

0
Alexandre Perez On BEST ANSWER

Use HtmlButton instead of Button if you want the "HTML button tag"

var btn = new HtmlButton();
btn.Attributes["class"] = "bla";
btn.Attributes["type"] = "button";

Button renders <input type="submit" /> and Button.UseSubmitBehavior renders <input type="button" />.

HtmlButton will render <button type="YOUR_DEFINITION"></button>.