我在ASP/VB.net中展示了一种奇怪的行为。我试图从VB代码后面调用一个JS,但是什么都没有发生。我在几个页面上使用了一个类似的(几乎相同的)方法,取得了很好的效果。然而,它在这个问题上行不通。我试图问用户一个“是”/“否”类型的问题。结果存储在一个名为confirm_value的隐藏字段中,如果用户回答“是/继续”,JS将再次单击该按钮。
<asp:HiddenField runat="server" id ="confirm_value"/>
然而,JS从不开火。我甚至尝试做一个简单的"Hi那里“警报调用,就像注释代码中的那样,它也不起作用。什么能阻止JS代码的发射?
VB代码:
If -1000 < RadNumericTextBox1.Text Then
If confirm_value.Value <> "Yes" Then
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the startup script is already registered.
If (Not cs.IsStartupScriptRegistered(Me.GetType(), "ConfirmScriptRXOrder")) Then
Dim cstext1 As String = "Confirm('This item has less in stock than your order. Do you want to order it anyway?');"
'Dim cstext1 As String = "alert('Hi there!');"
cs.RegisterStartupScript(Me.GetType(), "ConfirmScriptRXOrder", cstext1, True)
End If
'return here, JS will re-click submit button after okaying the message,.
Return
Else
confirm_value.Value = "No"
Mycart(ddCyl.SelectedValue.ToString, ddSph.SelectedItem.Text, ddCyl.SelectedItem.Text, RadNumericTextBox1.Text, tbtray.Text)
RadGrid1.DataBind()
End If
End If
“联合来文法典”:
<script type = "text/javascript">
function Confirm(message) {
if (confirm(message)) {
var x = document.getElementById('<%= confirm_value.ClientID%>');
x.value = 'Yes';
document.getElementById('<%=btnOrder.ClientID%>').click();
} else {
var x = document.getElementById('<%= confirm_value.ClientID%>');
x.value = 'No';
}
}
</script>
发布于 2017-07-27 16:51:58
我不想回答我自己的问题,但解决方案在我最初发布的内容中并不明显,与我发布的实际代码没有任何关系。我将页面简化为submit按钮、JS和VB调用。啊,真灵!我开始添加回代码直到它坏了。问题是按钮在asp:updatepanel中。我将按钮移到更新面板外,它可以按我的要求工作!看上去不太平衡但是..。所以,如果你有这样的情况,记住这篇文章!
发布于 2017-07-26 13:44:37
我不是爪哇专家..。
但是,您的java函数不应该被称为"ConfirmScriptRXOrder“而不是”确认“吗?
https://stackoverflow.com/questions/45334387
复制相似问题