【问题标题】:How can I set a Button's type to "Button" (as opposed to the default "Submit") in C#?如何在 C# 中将 Button 的类型设置为“Button”(而不是默认的“Submit”)?
【发布时间】:2015-09-04 20:30:46
【问题描述】:

根据here 的回答,我可以通过将其类型值设置为“按钮”来阻止按钮提交表单,就像在 HTML 中一样:

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

...但是如何在 C# 中做到这一点?我正在动态创建我的控件,就像这样(伪代码,因为我远离我的 IDE):

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

【问题讨论】:

  • Type的属性类型是什么??
  • 您还可以将 UseSubmitBehavior="false" 添加到 asp:Button 控件。

标签: c# dynamic sharepoint-2010 submit-button htmlbutton


【解决方案1】:

如果您想要“HTML 按钮标签”,请使用 HtmlButton 而不是 Button

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

Button 呈现 &lt;input type="submit" /&gt; 和 Button.UseSubmitBehavior 呈现 &lt;input type="button" /&gt;

HtmlButton 将呈现&lt;button type="YOUR_DEFINITION"&gt;&lt;/button&gt;

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多