【问题标题】:How to trigger OnClick and OnClientClick events simultaneously in ASP.NET when using jQuery?使用 jQuery 时如何在 ASP.NET 中同时触发 OnClick 和 OnClientClick 事件?
【发布时间】:2012-07-12 14:24:52
【问题描述】:

我试图在回发期间显示消息栏。我正在使用我在网上找到的 jQuery 脚本here。一切都很好,但是当 jQuery 添加到页面时,我的服务器端事件永远不会被调用。

这是我的按钮代码:

<asp:Button ID="btnGetReport" CssClass="float-left" runat="server" 
Text="<%$ Glossary:GetReport %>" OnClick="btnGetReport_Click" />

这是内联脚本:

<script type="text/javascript">
$(document).ready(function () {
    $('#<%=btnGetReport.ClientID%>').click(function (event) {
        event.preventDefault();
        $('#message_bar').displayMessage({
            position: 'fixed',
            skin: 'modern',
            message: 'We are fetching your report. Please wait...',
        });
    });
});
</script>

这是相关的外部 .js 文件:

(function( $ ){

$.fn.displayMessage = function(options) {

    // Default configuration properties.
     var defaults = {
              message       : 'Error message',
              speed         : 'fast',
              position      : 'fixed', // relative, absolute, fixed
              autohide      : true
     }

    var options = $.extend( defaults, options );
    $(this).slideUp('fast');
    $(this).removeClass().empty();
    return this.each(function() {

      var sticky = (options.sticky == false) ? 'relative' : 'absolute';
      $(this).addClass('messagebar-skin-'+options.skin+'_bar').css('position',options.position).css('background-color',options.background);
      $(this).append('<div class="messagebar-skin-'+options.skin+'_inner"><span class="messagebar-skin-'+options.skin+'_text"></span></div>').css('color',options.color);
      $(this).find('span').html(options.message);

      $(this).slideDown(options.speed ,function(){

         var parent = ($(this).attr('id')) ? "#"+$(this).attr('id') : "."+$(this).attr('class');
         var close_button = ($(this).find('a').attr('id')) ? "#"+$(this).find('a').attr('id') : "."+$(this).find('a').attr('class');

         if(options.autohide == true)
         {
            $(this).delay(2400).fadeOut('slow');
         }

         $(parent+">div>"+close_button).bind("click", function (event) {
            event.preventDefault();
            $(parent+">div>"+close_button).animate({"opacity": "hide"}, function(){
                $(parent+">div>span").fadeOut("slow").html("");
                $(parent+">div>"+close_button).parent().parent().slideUp(options.speed);
            });
         });

  });

});

};
})( jQuery );

我也尝试使用 OnClientClick 属性调用该函数,但似乎都不起作用。我查看了人们对此有疑问的其他示例,但似乎也没有任何帮助。

任何想法将不胜感激!

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    您是否尝试过类似的简单方法:

    OnClientClick="return YourJavaScriptFunction();"
    
    <asp:Button ID="btnGetReport" CssClass="float-left" runat="server" 
        Text="<%$ Glossary:GetReport %>" OnClick="btnGetReport_Click" 
        OnClientClick="return YourJavaScriptFunction();" />
    

    在你的 JavaScript 函数中最后返回 true

    function YourJavaScriptFunction () {
        // your cool stuff
        return true;
    }
    

    编辑 1

    这一行:

    OnClientClick="return YourJavaScriptFunction();"
    

    相当于:

    $('#<%=btnGetReport.ClientID%>').click(function () {
    

    您的脚本如下所示:

    <script type="text/javascript">
    
    function YourJavaScriptFunction (){
            $('#message_bar').displayMessage({
                position: 'fixed',
                skin: 'modern',
                message: 'We are fetching your report. Please wait...'
            });    
    
            // this will prevent the postback, equivalent to: event.preventDefault();
            return false;
    }
    
    </script>
    

    【讨论】:

    • 我对 jQuery 没有太多经验,所以我不完全确定该怎么做。如何进行函数调用并绕过内联脚本中的“$('#').click(function (event)”?
    • 我将按钮放入面板控件并添加了 DefaultButton 属性以允许用户按 Enter。 jQuery 函数会触发,但服务器端又不会触发。如果我点击它就可以正常工作,只是在按 Enter 时不行。有什么想法吗?
    • 我能够解决这个问题,以防有人遇到类似问题。我在 DefaultButton 上的原始 OnClientClick 属性是 OnClientClick="return testClick();"。我将其更改为 OnClientClick="if(!testClick()){return true;}" 效果很好!
    【解决方案2】:

    试试这个:

       <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=btnGetReport.ClientID%>').click(function (event) {
                event.preventDefault();
                $('#message_bar').displayMessage({
                    position: 'fixed',
                    skin: 'modern',
                    message: 'We are fetching your report. Please wait...',
                });
             __doPostBack("btnGetReport", "");
            });
        });
        </script>
    

    如果使用母版页,那么试试这个:

     __doPostBack("ctl00$MainContent$btnGetReport", "");
    

    【讨论】:

    • 我试过了,但没有用。回发发生了,但客户端和服务器端功能没有触发。不过感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多