【问题标题】:Calling JavaScript from Asp.Net CodeBehind(Not a function)从 Asp.Net CodeBehind 调用 JavaScript(不是函数)
【发布时间】:2011-11-20 16:41:33
【问题描述】:

我需要知道如何在 Asp.Net 中使用按钮单击事件调用下面的 Javascript

<script type='text/javascript'>
        $("#various3").fancybox({
            'width': '75%',
            'height': '75%',
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type': 'iframe'
        });
</script>

这是我正常的 Asp.Net 按钮

<asp:Button ID="Button1" runat="server" Text="Button" />

【问题讨论】:

    标签: javascript jquery asp.net button click


    【解决方案1】:

    将代码封装在一个函数中,并在按钮的客户端点击事件中调用该函数。

    <script type='text/javascript'>
       function createFancyBox() {
        $("#various3").fancybox({
            'width': '75%',
            'height': '75%',
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type': 'iframe'
        });
        // include this line to stop the button to call the server side event
        return false;
      }
    </script>
    
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return createFancyBox();" />
    

    【讨论】:

    • +1 但由于该函数将始终返回 false 并且单击此按钮不会回发任何内容;我会摆脱 asp:button 控件,只拥有一个常规的 button 元素。
    • @Waqas-For 按钮它不能正常工作,如果我使用超链接它工作良好,这可能是什么原因?
    猜你喜欢
    • 2011-08-09
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    相关资源
    最近更新 更多