我有一个带有表单的aspx页面,该表单包含一个asp按钮,在页面后面的代码中有一个单击处理程序。我希望能够单击表单上的按钮,调用单击处理程序,在后面的代码中执行逻辑,但是一旦单击处理程序事件完成,就不会返回响应/回发。
表格代码:
<%@ Page Language =" vb" AutoEventWireup ="假"代码隐藏=" ItemEdit.aspx.vb"
继承=" ItemEdit" %GT;
<!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>
代码背后: Imports System.Data.OleDb 进口System.IO Imports 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
答案 0 :(得分:0)
你必须使用Ajax。在这种情况下,最简单的方法是使用更新面板。
您必须在页面中添加ScriptManager:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
使用UpdatePanel控件将您想要的控件包装到Partial update:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
... table content from example
</table>
</ContentTemplate>
</asp:UpdatePanel>
还有其他方法可以在Asp.Net中使用Ajax,但这是最简单的方法。
有关在Asp.Net Webforms中使用Update Panel和Ajax的更多信息,请查看:
UpdatePanel控件简介: https://msdn.microsoft.com/en-us/library/bb399001(v=vs.100).aspx
UpdatePanel类: https://msdn.microsoft.com/pt-br/library/system.web.ui.updatepanel(v=vs.110).aspx