我在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