我在ASP/VB.net中显示了一个奇怪的行为。 我试图从后面的VB代码调用一个JS,但没有任何事情发生。我在几页上使用了类似(几乎相同)的方法,并取得了很好的效果。但是,这不适用于这一个。 我想问用户一个是/否类型的问题。结果存储到一个隐藏字段名为confirm_value,那么JS再次点击按钮,如果用户ansers“是/继续”无法从VB代码背后调用javascript代码
<asp:HiddenField runat="server" id ="confirm_value"/>
然而,JS永远不会触发。我甚至试着做一个简单的“Hi There”警报调用,就像在注释代码中一样,它也没有工作。 什么可以防止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
JS代码:
<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>
这是一个独特的名称,用于将调用注册到您基本上正在创建的脚本中。 “确认(blah,blah)”我弄清楚了我会发布的情况,所以每个人都有这个问题可以学习。 –
干得好,所以你解决了这个问题并发布了你的解决方案。 – Zeddy