Mark Clark
Mark Clark

Reputation: 177

Trouble calling javascript code from VB code behind

I'm expriencing an odd behavior in ASP/VB.net. I am attempting to call a JS from the VB code behind, but nothing is happening. I use a similar(almost identical) method on several pages with great results. However, it doesn't work on this one. I am trying to ask the user a yes/no type question. The result is stored to a hidden field named confirm_value, then the JS clicks the button again if the user ansers "yes/continue"

<asp:HiddenField  runat="server" id ="confirm_value"/>

However the JS never fires. I even tried making a simple "Hi There" alert call, as in the commented code, which did not work either. What could prevent the JS code from firing?

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>

Upvotes: 0

Views: 41

Answers (2)

Mark Clark
Mark Clark

Reputation: 177

I hate to answer my own question, but the solution was not obvious in what I originally posted, and had nothing to do with the actual code I posted. I stripped the page down to the submit button, JS, and VB call to it. It worked! I started adding back code until it broke. The issue was the button was inside an asp:updatepanel . I moved the button outside the update panel and it worked as I wanted! Didn't look quite as visually balanced, but... So if you ever have a situation like this, remember this post!

Upvotes: 1

Zeddy
Zeddy

Reputation: 2089

I'm no expert in java...

but shouldn't your java function be called "ConfirmScriptRXOrder" instead of "Confirm"?

Upvotes: 0

Related Questions