OnClick and onClick ?

Posted on 2006-05-07
Last Modified: 2011-12-19
I put an "OnClick" attribute into my WebControl, to call a process:

<asp:button id="Button1" runat="server" Text="Button" OnClick="myButtonModule"></asp:button></td>

So, what is the difference between this "OnClick" and the javascript "onClick" ?
Is this something that is run "server side", or what?
Any "Browser Implications"?
Question by:born4code
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    As by the documentation, it should be onClick:

    I would hence stick to that, as I don't know which browsers would complain/ignore OnClick ...
    LVL 1

    Expert Comment

    onClick for an <asp:Button> calls a function that you define in your <script runat="server"> block. (server-side code) While onClick for a <input type="button"> call a Javascript function (client-side code)
    LVL 1

    Expert Comment

    There's really no difference besides where the action is process, server vs. client
    LVL 2

    Accepted Solution

    the difference between the 2 are :

    In the end there is none. But when you use an server control this server controls "becomes alive"  on the server and certain events are raised on the server in relation to your control

    the rendering of the <asp:Button control results in a html tag that looks something like this <input type="button" onclick="__doPostBack('this','')" id="Button1" />

    the OnClick event gets rendered as an onclick tag.

    There are no browser implications. renders everything browser compatible. When you use server controls you don't have to worry about browser compatibilty. The guys from MS did that for you.
    LVL 6

    Assisted Solution

    Both can be used like this:
    <asp:button id="Button1" runat="server" Text="Button" OnClick="HandledInServer" OnClientClick="HnadeledInClientJavascript"></asp:button>

    OnClick will be treated as a call to internal javascript function to call the server (post backing)..
    but OnClientClick is to be used to do user javascript calls.. e.g. Delete button should be like this:
    <asp:button id="btnDelete" runat="server" Text="Delete" OnClick="DoDelete" OnClientClick="return confirm('Are you sure you want to delete?');"></asp:button>
    if u click on this button, first the OnClinetClick will be called.. and a confirmation box will pop up .. if you click yes, the calling of OnClick [which is calling javascript for function: '_doPostBack()' which will call the server to do delete operation.

    LVL 2

    Author Comment

    Thanks guys.  Your answers were only 20 minutes apart...

    nehaya, the "OnClientClick" attribute was a bonus, so I gave you some points for assist.

    Thanks again.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now