I have to resolve a problem in some code assigned to me and the problem is that following this code in page.initelse statemant:
Me.SaveChangesButton.Attributes.Add("OnClick",
"if (!confirm('Are you sure?')){
return false;
} else {
document.getElementById('SaveChangesButton').disabled = true;
document.getElementById('rejectButton').disabled = true;
}
")
overrides this code:
Protected Sub SaveChangesButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles SaveChangesButton.Click
Question [edited]
How can I bindconfirm()to button so that it doesn’t override myhandlerfunction?Where in the code would it be best practice to bind it?If I remove second partelse {document.getElementById('SaveChangesButton').disabled =it works correctly, but how should I write it so it continues to execute
true; document.getElementById('rejectButton').disabled = true;}SaveChangesButton_Click, as I understand only the second part overrides handler?- How can I execute those two lines
document.getElementById('SaveChangesButton').disabled = true;and still execute
document.getElementById('rejectButton').disabled = true;SaveChangesButton_Clickhandler without overriding it?
1) You should use OnClientClick:
2) If you bind the
SaveChangesButton_Clickin thePage.Init, I would then set theOnClientClicktoo.3,4) The
OnClientClickfires (sort of) before the server sideOnClick(of course). This makes you disabling your button, before firing theOnClick. AndOnClickwon’t fire on a disabled button. What to do? Delay disabling your button.And this actually makes your question a duplicate of this one…