Cancel button click with JavaScript

6 posts, 1 answers
  1. David Wilson
    David Wilson avatar
    4 posts
    Member since:
    Feb 2006

    Posted 18 Apr 2008 Link to this post

    What is the proper technique to cancel a button clicked on the RadToolBar.

    Simplistically, I'd like to do something like the following in code behind:

    Me.RadToolBar1.Items(1).Attributes.Add("onclick""if(!confirm('Are you sure you want to delete this item?')) return false;"

    But this JavaScript does not cancel the call back to the server.

    Any ideas?

    Thanks.

    --David
  2. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 21 Apr 2008 Link to this post

    Hello David Wilson,

    I suggest that you subscribe to the OnClientButtonClicking event and cancel it if the user does not confirm. 

    I hope this helps.

    Kind regards,
    Veskoni
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. terrysmith
    terrysmith avatar
    127 posts
    Member since:
    Nov 2006

    Posted 23 Apr 2008 Link to this post

    I am having the same problem. On a button click I want to fire some Javascript code and prevent a server postback.

    If I do this:

        function OnClientButtonClicking(sender, args)
        {       
            args.set_cancel(true);
           
            var button = args.get_item();
                   
            if (button.get_value() == "print")
            {
                window.print();
            }

    I get this error:

        Microsoft JScript runtime error: Object doesn't support this property or method

    on the args.set_cancel line. If I try canceling it this way it doesn't throw any errors but the server-side ButtonClick event is still hit:

        function OnClientButtonClicking(sender, args)
        {       
            var domEvent = args.get_domEvent();
           
            // Stop IE from showing its context menu.
            domEvent.cancelBubble = true;

            if (domEvent.stopPropagation)
            {
                domEvent.stopPropagation();
            }
                  
            domEvent.returnValue = false;   
                      
            var button = args.get_item();
                   
            if (button.get_value() == "print")
            {
                window.print();
            }
  5. Erjan Gavalji
    Admin
    Erjan Gavalji avatar
    1455 posts

    Posted 23 Apr 2008 Link to this post

    Hi guys,

    Please, find attached a small page, demonstrating the button click cancellation. Terry, could it be that you've actually subscribed to the OnClientButtonClicked event?

    Cheers,
    Erjan Gavalji
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. terrysmith
    terrysmith avatar
    127 posts
    Member since:
    Nov 2006

    Posted 23 Apr 2008 Link to this post

    You're right. I would have swore that I changed OnClientButtonClicked to OnClientButtonClicking, but after the leaving the code for awhile and then coming back to it I quickly saw that I had not changed it.


    Thanks!
    Terry

  7. Roberto
    Roberto avatar
    4 posts
    Member since:
    Mar 2008

    Posted 07 May 2008 Link to this post

    Hello,
    I resolve with a java confirm invoked in OnClientItemClicking

    <

    script type="text/javascript">

    function onMyClicked(sender, args)

    {

    var btn = args.get_item().get_value();

    if (btn == 'IDDelete') {

    var result = confirm("Are you sure you clicked: " + btn);

    if (! result){

    args.set_cancel(

    true);

    }

    }

    }

    </

    script>

    Kind regards

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017