strange "behavior" when using args.set_cancel(true)

5 posts, 0 answers
  1. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 29 May 2013 Link to this post

    I have been working with client script using RadButton, and noticed some odd behavior when calling args.set_cancel. During the OnClientClicking event, i check a condition, if true, button is allowed to postback, if false, set_cancel is set to true. I have two buttons using this event.

    function OnClientClicking(sender, args) {
        if (somecondition == false) {
            args.set_cancel(true);
        }
    }

    Now, if I click Button1, and somecondition is false, then the postback will cancel. However, if I click Button2 next, when the OnClientClicking event is fired, somecondition is true, and set_cancel(true) is NOT called, but... the button does not post back. Now if I reload the page and click Button2 first, then it posts back, until i click Button1, then Button2 no longer posts back.

    I tried explicitly calling args.set_cancel(false); 
    but it still does not work right.

    function OnClientClicking(sender, args) {
        if (somecondition == false) {
            args.set_cancel(true);
        } else {
            args.set_cancel(false);
        }
    }

    Any ideas what might be causing this weirdness?


  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 May 2013 Link to this post

    Hi Eric,

    I have tried a similar scenario and its working fine at my end. Here is the sample code I tried.

    ASPX:
    <telerik:RadButton ID="RadButton1" runat="server" Text="Button 1" OnClientClicking="OnClientClicking"
        OnClick="RadButton1_Click">
    </telerik:RadButton>
    <telerik:RadButton ID="RadButton2" runat="server" Text="Button 2" OnClientClicking="OnClientClicking"
        OnClick="RadButton2_Click">
    </telerik:RadButton>

    JavaScript:
    <script type="text/javascript">
        function OnClientClicking(sender, args) {
            if (sender._text == "Button 1") {
                args.set_cancel(true);
            }
        }
    </script>

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 30 May 2013 Link to this post

    well, I tried your code and yes, it works as expected, however... i realized something. I forgot to mention that i was using SingleClick=true and one button had causes validation set to false.

    <asp:TextBox ID="txtTest" runat="server" />
    <asp:RequiredFieldValidator ID="rfvtxtTest" ErrorMessage="Test is required" ControlToValidate="txtTest"
        runat="server" />
     
    <telerik:RadButton ID="RadButton1" runat="server" Text="Button 1" OnClientClicking="OnClientClicking"
        SingleClick="true" CausesValidation="true">
    </telerik:RadButton>
    <telerik:RadButton ID="RadButton2" runat="server" Text="Button 2" OnClientClicking="OnClientClicking"
        SingleClick="true" CausesValidation="false">
    </telerik:RadButton>


    the code above does exactly what I described. First button will have its set_cancel set to true, and button 2 will not trigger postback after button 1 is pressed.

    So I narrowed down the issue to validation. I have a textbox with a required field validator. Button 1 trips validation, Button 2 is clicked... no post back. If Button 2 is pressed, it will post back until Button 1 trips the validator, then Button 2 will stop posting back.

    Sorry, I should have mentioned the SingleClick and CausesValidation

    EDIT: One more thing. I know you're probably wondering, why use set_cancel? Well, I'm working on a custom button for our developers, and i need to cancel the OnClientClick event to prevent other script from running. I can do a work around in the meantime, but I figured I would point out this oddness.
  5. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1490 posts

    Posted 03 Jun 2013 Link to this post

    Hi Eric,

    I have successfully reproduce the unexpected behavior. This seems to be an issue with the control when a RadButton is clicked and the following conditions are met:
    • Page validation is not passed
    • RadButton's postback is cancelled in OnClientClicking event by setting args.set_cancel(true)

    which leads to the prevention of the postback of the RadButtons.

    I have logged the issue in our feedback portal here, so that you can monitor, comment and vote on it.

    I have also updated your Telerik points for reporting this issue to us.


    Regards,
    Danail Vasilev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 14 Jun 2013 Link to this post

    I also need a button to conditionally postback. Getting this to work correctly will solve many problems for me.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017