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";
}

RogerPosted Nov 10, 2022, 8:30 AM
Thank you ! Those obtuse minds that make the microsoft documentation should explain things like that... So much ado about "Microsoft Learn" but they keep doing sometimes, like if they don't want you to learn the easy way.
Bui TuongPosted Jun 27, 2014, 6:02 AM
thanks
Salman FarooquiPosted Jun 3, 2014, 5:03 AM
Great
Rajagopalan R VPosted Jun 3, 2014, 2:11 AM
Nice
Rajagopalan R VPosted Jun 3, 2014, 2:11 AM
Clear Difference Mr. Suresh M