Thursday, October 25, 2012

OnClientClickevent and AJAX is not working for telerik RadControls

One may need to provide a confirmation dialog or check some validation to the users and initiate an AJAX request if confirmation accepted or validation perform successful. Confirmation using standard post backs often looks like this: 

<asp:Button ID="btnSaveComplaint" runat="server" Text="Save" OnClick="btnSaveComplaint_Click" OnClientClick="return ValidateMacroText();" /> 
 Note: ValidateMacroText() is javascript function which returns true if validation perform successfully.



The OnClientClick should be changed a bit to work with AJAX:

<asp:Button ID="btnSaveComplaint" runat="server" Text="Save" OnClick="btnSaveComplaint_Click" OnClientClick="if(!ValidateMacroText()) return false;" /> 

When the button is placed within RadAjaxPanel control.

Alternatively, the OnRequestStart client-side event could be used to implement more complex logic.
Here is a sample script:
<script type="text/javascript">
    function OnRequestStart(ajaxControl, eventArgs) {
        var eventTarget = eventArgs.get_eventTarget();
        if (eventTarget == "<%= ImageButton1.UniqueID %>") {
            return confirm('Are you sure?');
        }
        else {
            return false;
        }
    }
</script>

No comments:

Post a Comment

^ Scroll to Top