从后面的代码调用javascript确认不工作

时间:2009-09-29 08:43:20

标签: javascript ajax code-behind

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim ResourceObject As Object


        Dim js As [String] = (vbCr & vbLf & " if(confirm('Esta Seguro de eliminar de la lista')==true)" & vbCr & vbLf & " document.getElementById('" & txtRespuesta.ClientID & "').value='true';" & vbCr & vbLf & " else" & vbCr & vbLf & "  document.getElementById('") + txtRespuesta.ClientID & "').value='false';" & vbCr & vbLf & " "
        ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Key", js, True)

  If txtRespuesta.Text = False Then
         -Action 1
  Else
             -Action 2
  End If

   End Sub



<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

<tr >
        <td  align="center" colspan="2">
          <asp:TextBox ID="txtRespuesta" runat="server" Width="174px" Height="21px"  
                MaxLength="20" style="font-weight: 700"  Font-Names="Verdana" TabIndex="2"></asp:TextBox>   
        </td>
    </tr> 
 <asp:Button ID="btnUpdate" runat="server"   Height="28px" 
                style="font-weight: 700" Text="変更" Width="68px" TabIndex="3" />
                  <asp:Button ID="btnDelete" runat="server" 
  </script>
        </ContentTemplate>
    </asp:UpdatePanel>

从后面的代码调用js

1 个答案:

答案 0 :(得分:0)

不,你不能直接从后面的代码调用Javascript代码。当前显示在浏览器中的页面与后面的代码之间没有直接连接。

由后面的代码创建的页面尚不存在,它将在后面的代码完成后存在,将HTML代码发送到浏览器,浏览器已解析它,并将其显示为新页面。

添加到页面的脚本将在页面发送到浏览器后运行,但您希望它立即运行,但这不会发生。

此外,您希望Javascript代码更改文本框的值,并将该值立即显示在服务器控件中。为了实现这一点,必须在将当前请求发送到浏览器之前及时发回该值,以便在创建控件时将该值读入控件。

您应该已在页面上显示确认代码,以便在将页面发回服务器之前在浏览器中运行它。发送回发后,在请求完成并在浏览器中加载新页面之前,在浏览器中执行任何操作都为时已晚。

相关问题