2017-07-26 77 views
0

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

回答

1

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

0

我在Java中没有专家...

,但应该不是你的Java功能,被称为“ConfirmScriptRXOrder“而不是”确认“?

+0

这是一个独特的名称,用于将调用注册到您基本上正在创建的脚本中。 “确认(blah,blah)”我弄清楚了我会发布的情况,所以每个人都有这个问题可以学习。 –

+0

干得好,所以你解决了这个问题并发布了你的解决方案。 – Zeddy