如何防止在后面的代码中单击按钮的响应?

How to prevent response on button click in code behind?

我有一个 aspx 页面,上面有一个表单,该表单包含一个 asp 按钮,该按钮在页面后面的代码中有一个点击处理程序。我希望能够单击表单上的按钮,调用单击处理程序,在后面的代码中执行逻辑,但不是 return a response/postback 一旦单击处理程序事件完成。

表单代码: <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ItemEdit.aspx.vb"
继承="ItemEdit" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Item Edit</title>
</head>
<body>
<form id="frmItemEdit" runat="server">
<table>
    <tr>
        <td style="height: 10px; width: 100px;">
        </td>
        <td style="height: 10px; width: 100px;">
        </td>
    </tr>
    <tr>
        <td style="height: 21px; width: 150px; text-align: right;">
            Item:
        </td>
        <td style="height: 21px; width: 200px;">
            <asp:Label ID="lblItem" runat="server" Width="200px">
</asp:Label>
            <asp:Label ID="lblItemId" runat="server" Visible="false">
</asp:Label>
        </td>
    </tr>
    <tr>
        <td style="height: 21px; width: 150px; text-align: right;"></td>
        <td style="height: 21px; width: 200px;">
            <asp:CheckBox ID="cbxMB" runat="server" Text="MB" 
/>
        </td>
    </tr>
    <tr>
        <td style="height: 21px; width: 150px; text-align: right;"></td>
        <td style="height: 21px; width: 200px;">
            <asp:CheckBox ID="cbxPP" runat="server" Text="PP" />
        </td>
    </tr>
    <tr>
        <td style="height: 21px; width: 150px; text-align: right;"></td>
        <td style="height: 21px; width: 200px;">
            <asp:CheckBox ID="cbxB" runat="server" Text="B" />
        </td>
    </tr>
    <tr>
        <td style="height: 21px; width: 150px; text-align: right;"></td>
        <td style="height: 21px; width: 200px;">
            <asp:CheckBox ID="cbxT" runat="server" Text="T" />
        </td>
    </tr>
    <tr>
        <td style="height: 10px; width: 100px;">
        </td>
        <td style="height: 10px; width: 100px;">
        </td>
    </tr>
    <tr>
    <td colspan="2" style="width:350px; text-align:center;">
    <asp:Button ID="btnSave" runat="server" Text="Save" Width="100px"/>
    </td>
    </tr>
</table>
</form>

背后的代码: 进口 System.Data.OleDb 进口 System.IO 进口 System.Data.SqlClient

Partial Public Class ItemEdit
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load
    If (Not Page.IsPostBack) Then
        If Request.QueryString("itemId") IsNot Nothing Then
            Dim itemId As String = Request.QueryString("itemId")
            Dim command As New OleDbCommand("SELECT * FROM items 
WHERE id = " & itemId, New OleDbConnection(ConnectionString))
            Try
                command.Connection.Open()
                Dim reader As OleDbDataReader = command.ExecuteReader()
                If reader.Read() Then
                    lblItem.Text = reader("item")
                    cbxMB.Checked = reader("mb")
                    cbxPP.Checked = reader("pp")
                    cbxB.Checked = reader("b")
                    cbxT.Checked = reader("t")
                    lblItemId.Text = reader("id")
                End If
                reader.Close()
            Finally
                command.Connection.Close()
            End Try
        End If
    End If
End Sub

Protected Sub btnSave_Click(ByVal sender As System.Object, ByVal e As  
System.EventArgs) Handles btnSave.Click
    Dim command As New OleDbCommand("UPDATE items SET b=" & 
Math.Abs(CInt(cbxB.Checked)) & ",t=" & Math.Abs(CInt(cbxT.Checked)) & 
",mb=" & Math.Abs(CInt(cbxMB.Checked)) & ",pp=" & 
Math.Abs(CInt(cbxPP.Checked)) & " WHERE id=" & lblItemId.Text, New 
OleDbConnection(ConnectionString))
    Try
        command.Connection.Open()
        command.ExecuteNonQuery()
    Finally
        command.Connection.Close()
    End Try
End Sub
End Class

你必须使用 Ajax。在这种情况下,最简单的方法是使用更新面板。

您必须将 ScriptManager 添加到您的页面:

<asp:ScriptManager ID="ScriptManager1" runat="server" />

并使用 UpdatePanel 控件包装您想要部分更新的控件:

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                    <table>
                         ... table content from example
                    </table>
            </ContentTemplate>
  </asp:UpdatePanel>

还有其他方法可以将 Ajax 与 Asp.Net 结合使用,但这是其中最简单的方法。

有关使用更新面板和 Ajax 与 Asp.Net 网络表单的更多信息,请查看:

更新面板控件介绍https://msdn.microsoft.com/en-us/library/bb399001(v=vs.100).aspx

更新面板Class: https://msdn.microsoft.com/pt-br/library/system.web.ui.updatepanel(v=vs.110).aspx