我正在尝试使用javascript确认消息验证文本框值。如果用户单击“确定”,则我希望将文本框值添加到列表框中,但如果用户单击“取消”,我想清除文本框而不将其添加到列表中。这在桌面应用程序中非常简单,但我正在尝试创建一个Web应用程序,而且我几周都在努力解决这个问题。
在textbox.textchanged事件中,我有这个代码,它首先进行检查以确保该值不在列表中(PublicCalls.ListContainsIndex),然后搜索表以确保该值已经不存在标记为计数(brlCounted)。如果已经计算过,用户应该提示他是否要继续重新计算该值或跳过它:
Protected Sub txtBrlKey_TextChanged(sender As Object, e As EventArgs) Handles txtBrlKey.TextChanged
Dim tmpBarrels As New cBarrels
Try
With Me
If Trim(.txtBrlKey.Text) <> "" Then
If Not PublicCalls.ListContainsIndex(Me, .lstBarrels, .txtBrlKey.Text) > -1 Then
If Me.Title = "frmBarrelMoves" And (PublicCalls.gPhysicalInventory Or PublicCalls.gCycleCount) Then ' called from the Physical Inventory or Cycle Count form (frmBarremMoves)
tmpBarrels = tmpBarrels.Search(Me, "spa_Barrel_Select '" & .txtBrlKey.Text & "'")
If LCase(tmpBarrels.item(1).BrlCounted) = LCase("Y") Then
If PublicCalls.MsgBoxConfirm(Me, "This Barrel ID has already been counted. Continue?") = False Then
txtBrlKey.Text = "" ' clear the control
Else
gBarrelEntered = True
UpdateBarrelList("I", .txtBrlKey.Text) ' add the barrel to the barrel list
gBarrelEntered = False
End If
End If
End If
End If
End If
End With
tmpBarrels = Nothing
Catch ex As Exception
PublicCalls.DisplayBblErrors(Me, ex, Nothing)
End Try
End Sub
MsgBoxConfirm的代码如下所示:
Public Shared Function MsgBoxConfirm(thePage As Page, Message As String) As Boolean
Dim HiddenConfirmResponse As HiddenField = CType(thePage.Master.FindControl("txtConfirmResponse"), HiddenField)
MsgBoxConfirm = False
HiddenConfirmResponse.Value = ""
' somehow this function needs to return true or false depending on if the user clicked OK or Cancel
' the problem is that ConfirmBox fires after the server side code finishes, which is too late to capture the OK or Cancel response here
Try
thePage.ClientScript.RegisterStartupScript(thePage.[GetType](), "ConfirmBox", (Convert.ToString("ConfirmBox('") & Message) + "');", True)
If HiddenConfirmResponse.Value = "OK" Then
MsgBoxConfirm = True
End If
Catch ex As Exception
PublicCalls.DisplayBblErrors(thePage, ex, Nothing)
End Try
End Function
javascript(在Site.Master标题中)如下所示:
<script type="text/javascript">
function ConfirmBox(message) {
var Answer = confirm(message);
if (Answer) {
document.getElementById('<%=txtConfirmResponse.ClientID%>').value = "OK";
} else {
document.getElementById('<%=txtConfirmResponse.ClientID%>').value = "Cancel";
}
}
</script>
隐藏的txtConfirmResponse在正文中定义:
<asp:HiddenField ID="txtConfirmResponse" runat="server" />
当确认框显示时,Code Behind已经移动,并且由于MsgBoxConfirm默认返回false,因此该值永远不会添加到列表中。如果我将默认值更改为True,则无论如何,该值始终会添加到列表中。我已经搜索和搜索,并且共识是客户端代码和服务器端代码之间的分离是原因,并且在我需要它时没有办法显示确认框 - 在将值添加到名单。如果是这种情况,那么我就不会看到确认框如何有用。我看到回答说,&#34;做不同的方式&#34;但怎么样?任何人都可以提供一个明确的例子,说明我需要如何更改此代码才能使其正常工作?我已经尝试过Ajax ModalPopupExtender,但它在显示确认框时没有任何区别。
非常沮丧。感谢您的任何帮助,您可以提供。 吉娜