无法在

时间:2017-07-26 18:03:22

标签: javascript asp.net vb.net

我在ASP / VB.net中表现出奇怪的行为。 我试图从后面的VB代码调用JS,但没有发生任何事情。我在几个页面上使用了类似(几乎相同)的方法,效果很好。但是,它并不适用于这个。 我试图向用户询问是/否类型问题。结果存储在名为confirm_value的隐藏字段中,然后如果用户激活"是/继续"则JS再次单击该按钮。

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

然而JS永远不会开火。我甚至试过制作一个简单的&#34; Hi There&#34;警报调用,如在注释代码中,也不起作用。 什么可以阻止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>

2 个答案:

答案 0 :(得分:1)

我讨厌回答我自己的问题,但解决方案在我最初发布的内容中并不明显,与我发布的实际代码无关。 我将页面剥离到提交按钮,JS和VB调用它。有效!我开始添加代码,直到它崩溃。 问题是按钮位于asp:updatepanel中。我在更新面板外移动了按钮,它按我的意愿工作!没有看起来像视觉平衡,但...... 所以如果你有这样的情况,请记住这篇文章!

答案 1 :(得分:0)

我不是java的专家......

但是你的java函数不应该被称为“ ConfirmScriptRXOrder ”而不是“确认”吗?