Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何提交按钮';s OnClick事件是否同时调用服务器端和客户端代码?_Javascript_Jquery_Asp.net_Twitter Bootstrap - Fatal编程技术网

Javascript 如何提交按钮';s OnClick事件是否同时调用服务器端和客户端代码?

Javascript 如何提交按钮';s OnClick事件是否同时调用服务器端和客户端代码?,javascript,jquery,asp.net,twitter-bootstrap,Javascript,Jquery,Asp.net,Twitter Bootstrap,我正在使用名为Metronic的引导模板。下面的asp:LinkButton是简单的按钮控件。为了能够调用服务器端事件处理程序(SubmitButton_Click),我将其转换为asp:LinkButton控件。但这次它不会调用javascript代码。我试过一次ClientClick。但是没有起作用 Login.aspx: <asp:LinkButton runat="server" ID="LinkButton1" CssClass="btn btn-default pull-ri

我正在使用名为Metronic的引导模板。下面的asp:LinkButton是简单的按钮控件。为了能够调用服务器端事件处理程序(SubmitButton_Click),我将其转换为asp:LinkButton控件。但这次它不会调用javascript代码。我试过一次ClientClick。但是没有起作用

Login.aspx:

 <asp:LinkButton runat="server" ID="LinkButton1" CssClass="btn btn-default pull-right"
  onclick="SubmitButton_Click">Log In<i class="m-icon-swapright m-icon-white"></i>
 </asp:LinkButton>

<script>
    jQuery(document).ready(function () {
        App.init();
        Login.init();
    });
</script>
Login.aspx.cs:

protected void SubmitButton_Click(object sender, EventArgs e)
{
   ..
}
Login.js:

var Login = function () {

return {
    //main function to initiate the module
    init: function () {

       $('.login-form').validate({
            errorElement: 'label', //default input error message container
            errorClass: 'help-inline', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                },
                remember: {
                    required: false
                }
            },

            messages: {
                username: {
                    required: "Username is required."
                },
                password: {
                    required: "Password is required."
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit   
                $('.alert-error', $('.login-form')).show();
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.control-group').addClass('error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.control-group').removeClass('error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                window.location.href = "/Default.aspx";
            }
        });

        $('.login-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.login-form').validate().form()) {
                    window.location.href = "index.html";
                }
                return false;
            }
        });

        $('.forget-form').validate({
            errorElement: 'label', //default input error message container
            errorClass: 'help-inline', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                email: {
                    required: true,
                    email: true
                }
            },

            messages: {
                email: {
                    required: "Email is required."
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit   

            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.control-group').addClass('error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.control-group').removeClass('error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                window.location.href = "index.html";
            }
        });

        $('.forget-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.forget-form').validate().form()) {
                    window.location.href = "index.html";
                }
                return false;
            }
        });

        jQuery('#forget-password').click(function () {
            jQuery('.login-form').hide();
            jQuery('.forget-form').show();
        });

        jQuery('#back-btn').click(function () {
            jQuery('.login-form').show();
            jQuery('.forget-form').hide();
        });

        $('.register-form').validate({
            errorElement: 'label', //default input error message container
            errorClass: 'help-inline', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                },
                rpassword: {
                    equalTo: "#register_password"
                },
                email: {
                    required: true,
                    email: true
                },
                tnc: {
                    required: true
                }
            },

            messages: { // custom messages for radio buttons and checkboxes
                tnc: {
                    required: "Please accept TNC first."
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit   

            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.control-group').addClass('error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.control-group').removeClass('error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                if (element.attr("name") == "tnc") { // insert checkbox errors after the container                  
                    error.addClass('help-small no-left-padding').insertAfter($('#register_tnc_error'));
                } else {
                    error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
                }
            },

            submitHandler: function (form) {
                window.location.href = "index.html";
            }
        });

        jQuery('#register-btn').click(function () {
            jQuery('.login-form').hide();
            jQuery('.register-form').show();
        });

        jQuery('#register-back-btn').click(function () {
            jQuery('.login-form').show();
            jQuery('.register-form').hide();
        });
    }

};

}();

使用jQuery选择器来处理由
LinkButton
呈现为HTML而导致的锚定点的单击,如下所示:

标记:

<asp:LinkButton runat="server" ID="LinkButton1" 
                CssClass="btn btn-default pull-right TheLinkButton"
                onclick="SubmitButton_Click">
    Log In<i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton>

记住在客户端单击
完成任何操作后返回true。或者,在客户端,您可以使用
\uuu doPostBack()
进行
回发
。或者,在服务器端,您可以使用类似
RegisterStartupScript
的方法调用客户端。返回false;是我的案子里少的那一块。谢谢但只适用于警报(“你好”)之类的东西。原始js代码仍然被忽略。我想是因为它被放在返回{}中。我不知道RegisterStartupScript是什么。它适用于警报(“Hello”)之类的东西。但它不适用于我需要的js代码。我认为它的问题在于,脚本被包装在一个返回{}中。Cos仍然忽略客户端。直接运行到服务器端
<asp:LinkButton runat="server" ID="LinkButton1" 
                CssClass="btn btn-default pull-right TheLinkButton"
                onclick="SubmitButton_Click">
    Log In<i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton>
jQuery(document).ready(function () {
    App.init();
    Login.init();
    jQuery('.TheLinkButton').click(function() {
        // Do what you need to do on the client-side 
        // when the link button is clicked here

    });
});