Difference Between Onclick and OnClientClick

This is a simple example to show the difference between OnClick and OnClientClick Event of asp: Button
 
OnClick will work on server side ,
 
OnClientClick will execute on client side before control passed to server or C# code
 
OnClientClick  will used to validate the controls like text box combo box etc...
 
If the Client Side code returns "TRUE" then it will go to server side code /"OnClick"  Event will work
 
If the Client Side code returns "FALSE" then OnClick will not fired / Event will not work 
 
 <asp:Button id="test" Text="Click Here" runat="server" OnClientClick="validate()" onClick="Button1_Click" />
 <asp:TextBox id="name" runat="server" />
 
function validate() {
var Firstname=document.getElementById('<%=name.ClientID%> ').value;
    if(Firstname=="")
      return false;
    else
    return true; 
protected void Button1_Click(object sender, EventArgs e)
{
    name.Text= "OnClick Work";
Ebook Download
View all
Learn
View all