简体   繁体   English

如何在 C# 中将 Button 的类型设置为“Button”(而不是默认的“Submit”)?

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

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:根据此处的答案,我可以通过将其类型值设置为“按钮”来阻止按钮提交表单,就像在 HTML 中一样:

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

...but how can I do this in C#? ...但是我怎么能在 C# 中做到这一点? I am creating my controls dynamically, something like this (pseudocode, as I'm away from my IDE):我正在动态创建我的控件,就像这样(伪代码,因为我远离我的 IDE):

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

Use HtmlButton instead of Button if you want the "HTML button tag"如果您想要“HTML 按钮标签”,请使用HtmlButton而不是 Button

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM