Using radconfirm() CallBackFunction to perform a postback

Thread is closed for posting
6 posts, 0 answers
  1. 63F75A2C-1F16-4AED-AFE8-B1BBD57646AD
    63F75A2C-1F16-4AED-AFE8-B1BBD57646AD avatar
    1572 posts
    Member since:
    Oct 2004

    Posted 06 Jul 2007 Link to this post

    Requirements

    RadControls version

    RadAjax, RadToolbar, RadWindow

    .NET version

    2.0

    Visual Studio version

    2005

    programming language

    C#

    browser support

    all browsers supported by RadControls


     
    PROJECT DESCRIPTION
    Unlike the standard confim(), radconfirm() cannot block the execution of the thread (this is something that cannot be done with Javascript) and a CallBackFunction is needed.

    The attached project demonstrates a real-life scenario where when clicking one of RadToolbar's buttons we want to ask a confirmation from the user before performing the postback. RadWindowManager and RadToolbar are wrapped in RadAjaxPanel to avoid full page's postback.

    The implementation of such scenario is relatively simple - when the user clicks on the button, we need to make a check what button is clicked and if it is the one which requires confirmation - cancel the postback and call radconfirm().
    Once the user confirms the postback, in the radconfirm's CallBackFunction we will use  __doPostBack to send the information on the server:

    <script type="text/javascript">  
        <%= RadToolbar1.ClientID %>.attachEvent("OnClientClick","Toolbar_ClientClick");  
                              
        function Toolbar_ClientClick(sender, e)  
        {  
            if (sender.CommandName == "Delete")  
            {  
                radconfirm("Delete?", CallBackFn);   
                return false;  
            }  
            else 
            {  
                return true;  
            }  
        }          
     
        function CallBackFn(arg)  
            {  
                if(arg)  
                    {  
                        __doPostBack("<%= RadToolbar1.UniqueID %>""onclick#Delete");  
                    }  
            }  
    </script> 


    in  __doPostBack we are provide the following arguments:
    1. The UniqueID of the RadToolbar - this will allow us to reference the toolbar even if a MasterPage is used
    2. The RadToolBar command that we want to be executed.

    And in the codebehind:

    protected void RadToolbar1_OnClick(object sender, Telerik.WebControls.RadToolbarClickEventArgs e)  
    {  
        Message.Text = "You clicked " +  e.Button.CommandName + " " + DateTime.Now.ToString("hh:mm:ss");  
  2. 37AF0C58-640E-4848-95F4-7135C9733106
    37AF0C58-640E-4848-95F4-7135C9733106 avatar
    2 posts
    Member since:
    May 2007

    Posted 05 Jun 2009 Link to this post

    For now, if you cannot get the radconfirm() working and want to use a regular confirm instead.. please see the code below. (until I figure out why the radconfigm is not showing)
    My situation is the following:
    1: User clicked a button Button1
    2: The Button1 event calls a web service which can either complete the processing or return an indication that the user needs to first make a decision based on the backend business rules.
    3: If the user clicks Yes, I initiate a PostBack via java script to call a different method Button2 event.

    Environment:
    VS2008
    RadControls: ASPNET Ajax Q1 2009

    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
     
    public partial class _Default : System.Web.UI.Page 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
             //   Message.Text = DateTime.Now.ToString("hh:mm:ss"); 
     
            } 
           // Message.Text = Request.Params["__EVENTARGUMENT"] + "  " + Request.Params["__EVENTTARGET"]; 
        } 
        
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            OpenNewWindow("http://google.com"); 
        } 
     
         public void OpenNewWindow(string url) 
        { 
            //string s = String.Format("<script>confirm('{0}');</script>", url); 
          //  string scp = "<script>radconfirm('Are you sure you want to continue?', CallBackFn);</script>"
           // string pb =  String.Format("<script>__doPostBack('{0}', 'onclick');</script>", Button2.UniqueID); 
            string pbbtn = String.Format("__doPostBack('{0}', 'onclick');", Button2.UniqueID); 
            string confPB = "<script>if(confirm('Are you sure?'))" 
                             + " { " 
                             + pbbtn 
                            + "} else { alert('confirm returned the following result: false');}" 
                            + "</script>"; 
     
            ClientScript.RegisterStartupScript(this.GetType(), "newWindow", confPB); 
        } 
        
     
        protected void Button2_Click(object sender, EventArgs e) 
        { 
            Button2.Text = DateTime.Now.ToString("hh:mm:ss"); 
     
        } 
     

  3. 37AF0C58-640E-4848-95F4-7135C9733106
    37AF0C58-640E-4848-95F4-7135C9733106 avatar
    2 posts
    Member since:
    May 2007

    Posted 05 Jun 2009 Link to this post

    If you have a master page, you can create an abstract base class which your MasterPage can extend and in your base class create the Confirm function as follows:

    =============Base Class===============
    /// <summary> 
            ///  
            /// </summary> 
            /// <param name="url"></param> 
            public void Confirm(string confirmMessage, Control callbackYes, Control callBackCancel) 
            { 
              //  string s = String.Format("<script>confirm('{0}');</script>", confirmMessage); 
              //  string scp = "<script>radconfirm('Called From Master Page?', CallBackFn);</script>"
              //  string pb = String.Format("<script>__doPostBack('{0}', 'onclick');</script>", callbackYes.UniqueID); 
                string pbbtn = String.Format("__doPostBack('{0}', 'onclick');", callbackYes.UniqueID); 
                string pbbtnCancel = String.Format("__doPostBack('{0}', 'onclick');", callBackCancel.UniqueID); 
                string confPB = String.Format("<script>if(confirm('{0}'))", confirmMessage) 
                                 + " { " 
                                 + pbbtn 
                                + "}" 
                                + "else {" 
                                + pbbtnCancel 
                                + "}" 
                                + "</script>"; 
     
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "__Confirm1", confPB); 
            } 
     
            /// <summary> 
            ///  
            /// </summary> 
            /// <param name="confirmMessage"></param> 
            /// <param name="callbackYes"></param> 
            public void Confirm(string confirmMessage, Control callbackYes) 
            { 
                string pbbtn = String.Format("__doPostBack('{0}', 'onclick');", callbackYes.UniqueID); 
                string confPB = String.Format("<script>if(confirm('{0}'))", confirmMessage) 
                                 + " { " 
                                 + pbbtn 
                                + "}" 
                                + "</script>"; 
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "__Confirm2", confPB); 
            } 
     

    ==================Master Page ============
    public partial class Site : InterfaceMaster 
        { 
            protected void Page_Load(object sender, EventArgs e) 
            { 
     
            }                
        } 


    =================Default.aspx==============
     protected void Button1_Click(object sender, EventArgs e) 
        { 
            MasterPage mp = Page.Master; 
            if (mp is TestingRadConfirm.InterfaceMaster) 
            { 
                (mp as TestingRadConfirm.InterfaceMaster).Confirm("Repointing this PBX will repoint all DIDs in the range. Are you sure you want to continue?", Button2, Button3); 
            } 
        } 
     
        /// <summary> 
        ///  
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
         
        protected void Button2_Click(object sender, EventArgs e) 
        { 
            Button2.Text = DateTime.Now.ToString("hh:mm:ss"); 
            Message.Text = "Button2 click event " + DateTime.Now.ToString("hh:mm:ss"); 
        } 
     
        /// <summary> 
        ///  
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void Button3_Click(object sender, EventArgs e) 
        { 
            Button3.Text = DateTime.Now.ToString("hh:mm:ss"); 
            Message.Text = "Button3 click event " + DateTime.Now.ToString("hh:mm:ss"); 
        } 

  4. 1B4AA8BB-D6FC-43BE-8DDF-52E8AB15DC08
    1B4AA8BB-D6FC-43BE-8DDF-52E8AB15DC08 avatar
    6 posts
    Member since:
    Apr 2012

    Posted 01 Mar 2014 Link to this post

    This is awesome thanks for posting it. I'm implementing it and I'm getting an error in the __doPostBack : "Input string was not in a correct format."

    So basically the Confirmation window shows up corrently.
    If i click cancel nothing happens (that's good)
    If I click OK, the pages does a postback and I get that error.

    So this is failing: __doPostBack("<%= MyRequestMenu.UniqueID %>", "onclick#Check In Metadata");
    I'm thinking is the parameters where I specify the command.

    Any ideas?


  5. A1CE16C4-4C2E-464E-BF18-532525D276CA
    A1CE16C4-4C2E-464E-BF18-532525D276CA avatar
    5948 posts
    Member since:
    Apr 2022

    Posted 05 Mar 2014 Link to this post

    Hello Karla,

    This is quite an old sample prject and it is likely that the controls have changed since then.
    What I can suggest is the following:

    - examine this demo on the way to integrate a RadConfirm, to see how to initially cancel, and then finish the JS task: http://demos.telerik.com/aspnet-ajax/window/examples/confirmserverclicks/defaultcs.aspx. Examine its description and code carefully to see the logic. You can raise a flag whether the action ws confirmed and click() the current button via the RadToolBar API: http://www.telerik.com/help/aspnet-ajax/toolbar-client-side-basics.html.

    - OR, store the command name in a hidden field instead of using the RadToolBar as an initiator. You can invoke a simple postback and check for the hidden field value in the Page_Load event to do the needed work.


    Regards,
    Marin Bratanov
    Telerik

    Consider using RadControls for ASP.NET AJAX (built on top of the ASP.NET AJAX framework) as a replacement for the Telerik ASP.NET Classic controls, See the product support lifecycle here.

  6. BF4F25F3-615A-4695-9D18-A5B149C0FBE6
    BF4F25F3-615A-4695-9D18-A5B149C0FBE6 avatar
    1 posts
    Member since:
    Nov 2013

    Posted 01 Dec 2016 Link to this post

      I used the Radconfirm at client side  while changing the  Page index  in batchEditing
                function ChangingthePageNo(sender, args) {
                    if (sender.get_batchEditingManager().hasChanges(sender.get_masterTableView())) {
                        var UpdatePanelCallbackFn = Function.createDelegate(sender, function (btnYesNo) {
                            if (btnYesNo) {
                                sender.get_batchEditingManager().saveChanges(sender.get_masterTableView());
                                args.set_cancel(true);
                            }
                            else {
                                args.set_cancel(true);
                            }
                        });
                        radconfirm("Are you sure want to save the  data to move next page?", UpdatePanelCallbackFn, 400, 180, null, "Confirm");
                        args.set_cancel(true);
                    }
                }
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.