I am using bootstrap template called Metronic. The asp:LinkButton below was simple button control. Just to be able to invoke the ServerSide event-handler (SubmitButton_Click), I converted it into asp:LinkButton control. But this time it doesn't invoke the javascript code. I tried OnClientClick. But didn't work.
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>
Part of 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: {
..
..
}
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();
});
}
};
}();
Use a jQuery selector to handle the click of the anchor that results from the LinkButton
being rendered to HTML, like this:
Markup:
<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>
Note: The
CssClass
value has hadTheLinkButton
added to it to allow for a simpler jQuery selector to be made using the dot (.
) notation as opposed to an ID (#
) selector. The main reason for this is that when using ASP.NET with master pages, the naming containers mangle the IDs of controls, while class names are unaffected.
JavaScript:
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
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.