当asp.net按钮单击事件完成时运行jquery函数

时间:2015-02-19 06:18:35

标签: jquery asp.net

我在asp.net按钮点击事件中写了一个简单的代码,例如我写这个:

Label1.text="Hello Stackoverflow!";

我希望当代码完成时,调用我的jquery函数,进行大量解释:
当asp.net按钮点击源完成时,运行我的jquery函数并向用户发出警报 我在html页面中的jquery代码是:

(function ($) {
        // DOM Ready
        $(function () {
            // Binding a click event
            // From jQuery v.1.7.0 use .on() instead of .bind()
            $('#Button1').bind('click', function (e) {
                // Prevents the default action to be triggered. 
                e.preventDefault();
                // Triggering bPopup when click event is fired
                $('#element_to_pop_up').bPopup();
            });
        });
    })(jQuery);

感谢。

2 个答案:

答案 0 :(得分:1)

实现这一目标的一种方法是使用ajax事件。

 $.ajaxSetup({
        beforeSend: function (jqXHR, settings) {
            //functions to be called before request is sent
        },
        complete: function () {
            // Register a handler to be called when Ajax requests complete
        },
      ajaxSuccess: function()
{
///call function after success of request
}
    });
});

调用弹出窗口应该是成功事件还是完成事件。

答案 1 :(得分:0)

试试这个

使用ScriptManager.RegisterStartupScript - 它将使用ScriptManager控件注册启动脚本块,并将脚本块添加到页面。

    protected void btnSave_Click(object sender, EventArgs e)
    {
       Label1.text="Hello Stackoverflow!";
       ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertFunction", "alert('Your message here...');", true);
// OR
 ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertFunction", "JqueryFunctionName()", true);

    }
  • 在.aspx文件中

<script type="text/javascript">

     function JqueryFunctionName(){
           $('#Button1').trigger("click"); // REMOVE THIS LINE
          $('#element_to_pop_up').bPopup(); // ADD THIS LINE
     }
</script>