如何使按钮不提交

时间:2014-08-25 16:46:57

标签: asp.net forms button

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

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

在检查元素时,我在DOM

中得到了这个

enter image description here

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

修改

完整的代码集。相关部分。

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

2 个答案:

答案 0 :(得分:1)

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

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

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

有关详细信息,请参阅this post

答案 1 :(得分:0)

不会使用更清洁的方法......

UseSubmitBehavior="false"

您还可以添加...

CausesValidation="false"

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

相关问题