【问题标题】:How to make a button not submit如何使按钮不提交
【发布时间】:2014-10-18 21:08:44
【问题描述】:

我知道创建的按钮都是提交类型。所以我使用按钮类的.CommandName 属性使其不是suggested here 的提交类型我制作按钮的方式是这样的

ePanel.Controls.Add(New Button With {.CssClass = "button enlargeMapButton", 
.Text = "Enlarge Map", .CommandName = "Sort"}) 

在检查元素后,我在 DOM 中得到了这个

单击按钮实际上会尝试提交表单并重新加载表单部分。我在这里遗漏了什么吗?

编辑

全套代码。相关部分。

Private Shared Function AddPropertyMap(ByVal currentTable As Table, ByRef 
propertyMapValue As Boolean) As Table
  ***Stuff createing other thing cells rows table etc***
  Dim MapControl As New Panel() With {.ID = "MapControl", .Visible = True, .CssClass = "MapBlock"}
  Dim ePanel As New Panel
  ePanel.Controls.Add(New Button With {.CssClass = "button enlargeMapButton", .Text = "Enlarge Map", .CommandName = "Sort"})
  MapControl.Controls.Add(ePanel)
  **creating Other control/things to other cell**
  Dim tCell3 As New TableCell()
  tCell3.Controls.Add(MapControl)
  Dim tRow As New TableRow()
  tRow.Cells.Add(tCell1)
  tRow.Cells.Add(tCell2)
  tRow.Cells.Add(tCell3)
  Dim newTable As New Table
  newTable.Rows.Add(tRow)
  Return newTable

【问题讨论】:

  • 您能否提供在 aspx 文件中声明按钮的位置的完整代码?我认为这将有助于确定问题所在......
  • 它实际上是在一个 Visual Basic 项目的 .vb 文件中制作的。这是我正在维护/用于“构建”表单的旧代码
  • 这可能会有所帮助 - stackoverflow.com/questions/683746/… - 即将 OnClientClick 属性/属性设置为 "return false"
  • 我建议你使用asp.net标签。
  • 您希望您的按钮仍然执行回发,但不作为表单提交按钮,还是希望它保持“仅限客户端”按钮?

标签: asp.net forms button


【解决方案1】:

难道不是更清洁的方法是使用...

UseSubmitBehavior="false"

您还可以添加...

CausesValidation="false"

防止按钮向页面添加一些不必要的标记。

【讨论】:

    【解决方案2】:

    您可以通过从 OnClientClick 处理程序返回 false 来阻止 Button 回发。在最简单的情况下,您只需将.OnClientClick 属性设置为"return false"

    ePanel.Controls.Add(New Button With {.CssClass = "button enlargeMapButton", .Text = "Enlarge Map", .CommandName = "Sort", .OnClientClick = "return false"})
    

    这也可以调用返回 false 的客户端 JavaScript 函数来抑制回发。

    更多详情请见this post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-06
      • 2011-06-29
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2014-08-04
      • 2019-06-29
      相关资源
      最近更新 更多