ASP.Net Button’s OnClientClick event


There are occasions that we need to check whether the fields in the page have been filled before executing the button clicks. You can use required field validators for this. Also you can use the above mentioned function of the button.

This can call a custom javascript function which you can write and depending on the value that you get from the function you can determine whether to proceed or not. An example of this is given below. In this example we are checking the length of the text entered to a field.

<asp:Button ID="cancelButton" runat="server"  onclick="cancelButton_Click" Text="Cancel" Width="160" OnClientClick="return confirmationOfCancellation()"  />


function confirmationOfCancellation() {

var field = document.getElementById('cancellationReasonTextBox');
if (field.value.length > 0) {
if (!confirm(‘Proceed’)) {
                    return false;
                }
                return true;
            }
            else {
                alert(‘You will not proceed’);
                return false;
            }
        }


Comments

Popular posts from this blog

Error message “User does not have send-as privilege.” when sending emails programmatically in CRM 2011