Using RadConfirm In Server Code
The RadConfirm dialog is essentially a RadWindow, which means it is entirely a client-side object. It can be manipulated via JavaScript on the client-side only. This means that when called from the server it can be shown to the end user only once all the server code has ran and the response is received in the browser. Logic that requires the user's confirmation during execution cannot be achieved directly with the RadConfirm, nor with a standard confirm() and thus it must be separated in two parts - the first one ends with the confirmation call, while the second part receives the response and takes the necessary action. Ultimately this will require an additional request to the server to send the user's choice and this is done in the RadConfirm's callback function because it receives the true/false result as an argument. There, depending on the result, a request can be initiated to finish the work. There are different ways to achieve it, for example:
Initiating A Postback
A hidden button with the desired event handler can be placed on the page and the __doPostBack() function can be used to call it. There are different ways to use it and some of them are listed in the Confirm Server Clicks Online Demo or in this article in the net. For example:
<telerik:RadWindowManager RenderMode="Lightweight" runat="server" ID="RadWindowManager1"></telerik:RadWindowManager>
<asp:Button ID="Button1" Text="call the RadConfirm" OnClick="Button1_Click" runat="server" />
<asp:Button ID="HiddenButton" Text="" Style="display: none;" OnClick="HiddenButton_Click" runat="server" />
<script type="text/javascript">
function confirmCallbackFn(arg)
{
if (arg) //the user clicked OK
{
__doPostBack("<%=HiddenButton.UniqueID %>", "");
}
}
</script>
The above example can contain an UpdatePanel around the hidden button in order to invoke an AJAX request instead of a full postback. An alternative is to use the RadAjaxManager and its events for the purpose:
<telerik:RadWindowManager RenderMode="Lightweight" runat="server" ID="RadWindowManager1"></telerik:RadWindowManager>
<telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" OnAjaxRequest="OnAjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="HiddenField_ConfirmResponse" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<asp:HiddenField runat="server" ID="HiddenField_ConfirmResponse" />
<asp:Button ID="Button2" Text="call the RadConfirm" OnClick="Button2_Click" runat="server" />
<script type="text/javascript">
function confirmCallbackFn(arg)
{
$get("<%=HiddenField_ConfirmResponse.ClientID %>").value = arg;
$find("<%=RadAjaxManager1.ClientID %>").ajaxRequest("confirmCallBack");
}
</script>
Using Page Methods
An alternative is to use Page methods to finish the task as they also provide an easy feedback mechanism for the user. This article can be a good starting point for using them. Note that you need to set the EnablePageMethods property of the script manager to true to enable them and to also include a reference to System.Web.Services.
<asp:ScriptManager ID="Scriptmanager1" runat="server" EnablePageMethods="true" />
<telerik:RadWindowManager RenderMode="Lightweight" runat="server" ID="RadWindowManager1">
</telerik:RadWindowManager>
<asp:Button ID="Button3" Text="call the RadConfirm" OnClick="Button3_Click" runat="server" />
<script type="text/javascript">
function confirmCallbackFn(arg)
{
PageMethods.CompleteTask(arg, OnSucceeded, OnFailed);
}
function OnSucceeded()
{
alert("Operation succeeded");
}
function OnFailed(error)
{
alert(error.get_message());
}
</script>
This approach allows you to pass more than one argument back to the server (depending on the number of arguments the server method's signature has) and to show feedback to the user easily, all encapsulated in a lightweight request.
Using A Custom RadWindow And Its ContentTemplate
Another approach is to use a RadWindow to mimic the built-in RadConfirm dialog - set its size, modality, behaviors (and other properties) as desired and copy the ConfirmTemplate from this article inside its ContentTemplate and modify it as desired. This allows for fine tuning of each aspect of the behavior of the dialog box and to also use any combination of the above logic. Also, handlers for the buttons can be added easily since server controls can be used in this case (for example RadButtons):
<telerik:RadWindowManager RenderMode="Lightweight" runat="server" ID="RadWindowManager1">
<Windows>
<telerik:RadWindow RenderMode="Lightweight" ID="rw_customConfirm" Modal="true" Behaviors="Close, Move" VisibleStatusbar="false"
Width="300px" Height="200px" runat="server">
<ContentTemplate>
<div class="rwDialogPopup radconfirm">
<div class="rwDialogText">
<asp:Literal ID="confirmMessage" Text="" runat="server" />
</div>
<div>
<telerik:RadButton RenderMode="Lightweight" runat="server" ID="rbConfirm_OK" Text="OK" OnClick="rbConfirm_OK_Click">
</telerik:RadButton>
<telerik:RadButton RenderMode="Lightweight" runat="server" ID="rbConfirm_Cancel" Text="Cancel" OnClientClicked="closeCustomConfirm" AutoPostback="false">
</telerik:RadButton>
</div>
</div>
</ContentTemplate>
</telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>
<asp:Button ID="Button4" Text="call the RadConfirm" OnClick="Button4_Click" runat="server" />
<script type="text/javascript">
function closeCustomConfirm()
{
$find("<%=rw_customConfirm.ClientID %>").close();
}
</script>
Using this directly will cause a full postback when OK button is clicked which will also close this RadWindow. Alternatively, to use AJAX,an UpdatePanel can be added around the HTML template and an additional JavaScript function can close the RadWindow.