This is a migrated thread and some comments may be shown as answers.

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

4 Answers 1072 Views
Button
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 29 May 2013, 01:25 PM
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?


4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 May 2013, 07:01 AM
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.
0
Eric
Top achievements
Rank 1
answered on 30 May 2013, 12:33 PM
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.
0
Danail Vasilev
Telerik team
answered on 03 Jun 2013, 12:15 PM
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.
0
Keith
Top achievements
Rank 2
answered on 14 Jun 2013, 01:39 PM
I also need a button to conditionally postback. Getting this to work correctly will solve many problems for me.
Tags
Button
Asked by
Eric
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Eric
Top achievements
Rank 1
Danail Vasilev
Telerik team
Keith
Top achievements
Rank 2
Share this question
or