How to suppress server control event (onclick)
Hello folks,
I have a simple form with a button. The button is server control.
in the OnClientclick event of the button control, I am actually doing some validations. If the validation fails, the onclick event should not be executed. How can I control this?
In the code below, PerformChecks() function gets fired first and alert message pops up fine. But immediately onclick event (which is handled on the server) gets triggered. if the check in PerformChecks fail, how can I suppress onclick event?
Code:
<asp:Button ID="ButtonRun" runat="server" Text="Go"
onclick="ButtonRun_Click" OnClientClick="PerformChecks();" Width="57px" />
function PerformChecks() {
var checkboxCollection = document.getElementById('<%=CheckBoxListInstruments.ClientID %>').getElementsByTagName('input');
for (var i = 0; i < checkboxCollection.length; i++) {
if (checkboxCollection[i].checked)
return;
}
window.alert("Please check atleast one checkbox");
}
}
Re: How to suppress server control event (onclick)
Put a
return false;
after the window.alert
Re: How to suppress server control event (onclick)
Are you actually doing work in the Server Side OnClick event? What I am getting at is, do you need the Button to be an ASP.Net Server control?
On another note, in order to tie with the other validation functionality that exists in ASP.Net, you might want to think about creating your own Custom Validator:
http://www.4guysfromrolla.com/articles/073102-1.aspx
Here is a complete sample that shows how you can do just that for the case of a CheckBoxList, which is what you are using:
http://www.4guysfromrolla.com/webtec...040302-1.shtml
Hope that helps!
Gary
Re: How to suppress server control event (onclick)
szlamany,
Thanks much. It worked like a charm.
Re: How to suppress server control event (onclick)
Gary,
Yes I have to use server control, because I doing lot of stuff on the server.
Thanks for your response
Re: How to suppress server control event (onclick)
In which case, that makes sense then :)
Can you remember to mark your thread as resolved?
Thanks
Gary