0

I am trying to fire ServerClick event in .NET on button click. it works fine if I don't add onclick="return ValidateForm();"

<button runat="server" id="btn1" type="submit" onclick="return ValidateForm();">SAVE</button>

I have already declared a javascript function in head as below:

<script>
    function ValidateForm(){
        //some validation code, return true/false
        return true;
    }
</script>

Below button also works fine:

<button runat="server" id="btn1" type="submit" onclick="return true;">SAVE</button>

but if I add onclick="return somefunction" its not working.

What I am doing wrong ?

Thanks

4
  • How is the title related to your question? Commented Aug 2, 2014 at 11:38
  • Sorry, system picked that wrong title from cookies and I did not noticed it. I have changed the title. Commented Aug 2, 2014 at 11:41
  • did u try add your function inside $(document).ready(); Commented Aug 2, 2014 at 12:53
  • set type as well in script tag <script type="text/javascript"> Commented Aug 2, 2014 at 12:57

1 Answer 1

1

change your markup to:

<button runat="server" id="btn1" type="submit" onclick="if( !ValidateForm() ) return false;" onserverclick="btn1_click">SAVE</button>

<script>
function ValidateForm() {
    //some validation code, return true/false
    return true;
}
</script>
  • don't forget that semi-colon at end of onlick attribute; If you inspect the result HTML code you will know why !

Assuming you have the click handler in code behind C# :

protected void btn1_click(object sender, EventArgs e)
{
}

or VB:

Protected Sub btn1_click(sender As Object, e As System.EventArgs)
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.