使用 jQuery 时如何在 ASP.NET 中同时触发 OnClick 和 OnClientClick 事件?

jfi*_*dof 5 javascript asp.net jquery

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

这是我的按钮代码:

<asp:Button ID="btnGetReport" CssClass="float-left" runat="server" 
Text="<%$ Glossary:GetReport %>" OnClick="btnGetReport_Click" />
Run Code Online (Sandbox Code Playgroud)

这是内联脚本:

<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>
Run Code Online (Sandbox Code Playgroud)

这是相关的外部 .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 );
Run Code Online (Sandbox Code Playgroud)

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

任何想法将不胜感激!

Jup*_*aol 5

您是否尝试过简单的事情,例如:

OnClientClick="return YourJavaScriptFunction();"

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

并在 JavaScript 函数true末尾返回

function YourJavaScriptFunction () {
    // your cool stuff
    return true;
}
Run Code Online (Sandbox Code Playgroud)

编辑1

这行:

OnClientClick="return YourJavaScriptFunction();"
Run Code Online (Sandbox Code Playgroud)

它相当于:

$('#<%=btnGetReport.ClientID%>').click(function () {
Run Code Online (Sandbox Code Playgroud)

你的脚本看起来像:

<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>
Run Code Online (Sandbox Code Playgroud)