Recently, I have encountered a strange issue while working with ASP/VB.net. In my code behind VB file, I am trying to call a JavaScript function, but it seems like nothing is happening. Surprisingly, I have used a very similar method on several other pages successfully, but for some reason, it fails to work on this particular page. My main goal is to prompt the user with a yes/no type question. The response is then saved in a hidden field named confirm_value, after which the JS script should click a button again if the user answers "yes" or chooses to continue.
<asp:HiddenField runat="server" id ="confirm_value"/>
Unfortunately, the JavaScript function never seems to execute. I even attempted a basic alert message saying "Hi There", although that didn't trigger either. What could be causing the JS code not to run?
VB Code:
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 Code:
<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>