Server Side MsgBox User Control for radconfirm, radalert and radprompt

62 posts, 0 answers
  1. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 22 Jan 2009 Link to this post

    Requirements

    RadControls version

     RadWindow for ASP.NET AJAX

    .NET version

    2.0

    Visual Studio version

    2008

    programming language

    VB.NET

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    The attached user control (I called it RadMsgBox) provides a user control with methods for launching radprompt, radalert and radconfirm from a server code behind page. I found that running these controls from client code only too restricting,

    To use this control, simply add a single reference to your page (example attached) and add the control library to your project. Then, on the code-behind page, you can call methods for multiple rad client windows directly from the server from a single instance of this control.

    It's important that your page has a RadWindowManager prior to the instansiation of the control on the page.

    The following methods have been defined:

    SetConfirmation - attach a radconfirm window to a control such as an ASP button
    SetPrompt - attach a radprompt window to a control such as an ASP button
    SetAlert - attach a radalert window to a control such as an ASP button

    ShowConfirmation - call radconfirm directly inline from server side code
    ShowPrompt - call radprompt directly inline from server side code
    ShowAlert - call radalert directly inline from server side code

    Note that an important feature of this control is its ability to call multiple instances of radconfirms and radprompts from a single RadMsgBox (feel free to change the name telerik folks ;-) by utilizing a "key" string value that is interrogated via a server postback.

    This has helped me out a lot, I hope someone finds it useful.

    -David

    NOTE: I have removed an reference in the projects attached to Telerik.Web.UI.dll, so you need to
    use your own licensed copy ;-)


  2. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 23 Jan 2009 Link to this post

    Hi David,

    Thank you for your contribution - I am sure others will find this article very helpful :)




    Regards,
    Georgi Tunev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Luis
    Luis avatar
    2 posts
    Member since:
    Jul 2008

    Posted 23 Feb 2009 Link to this post

    hi David.

    does it work with ajaxfied control?

    cheers.

  4. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 24 Feb 2009 Link to this post

    As a matter of fact, it does NOT. I realized this about 1 day after I posted this project.

    The code, in its current form, executed outside the Ajax framework and doesn't load properly where it's
    supposed to: basically, it doesn't work properly.

    So, I re-wrote it from scratch to work properly with either AJAX or non-AJAX controls (both using the Telerik
    framework/AJAX manager).

    If I can figure out how to post the source code, I will do so.

    -David
  5. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 25 Feb 2009 Link to this post

    Hi guys,

    Please find attached the updated demo that David sent us.

    Kind regards,
    Georgi Tunev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Acadia
    Acadia avatar
    203 posts
    Member since:
    May 2005

    Posted 02 Apr 2009 Link to this post

    Correct me if I'm wrong, but this cannot be used on a control (.ascx).  If it's possible could you please let us know how?
  7. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 02 Apr 2009 Link to this post

    It most definitely can be used in a user control. Make sure to provide a unique name to the control (e.g. RadMsgBox_myusercontrol1) as you wouldn't want to use RadMsgBox1 (for example) in multiple controls on the same page.
  8. James Legan
    James Legan avatar
    54 posts
    Member since:
    Sep 2008

    Posted 14 Apr 2009 Link to this post

    David,

    The first thing I noticed that I am never breaking on my Yes or No's even though the postback is indeed occuring. I looked at the defintion for the RadMsgBox tag on my page and I noticed that in your example, you are not specifying event handlers for OnYesSelected, OnNoSelected or OnPromptResponse. I added one for OnYesSelected and the following compiler error is thrown.

    Compiler Error Message: CS0426: The type name 'RadMsgBox' does not exist in the type 'RadMsgBox.RadMsgBox'  
     
    Source Error:  
     
       
     
    Line 45:     </style> 
    Line 46:     <telerik:RadWindowManager ID="RadWindowManager1" runat="server" /> 
    Line 47:     <RadMsgBox:RadMsgBox ID="rmbRadMsgBox"  OnYesSelected="rmbRadMsgBox_YesSelected" runat="server" /> 
    Line 48:     <%--<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Width="900px" LoadingPanelID="ralpLoadingPanel">--%> 
    Line 49:     <asp:Panel runat="server" ID="ContentPanel">  
       
     

    So I added all of my event handers programatically on Page_Init and everything works as exepected (I am not well versed in VB.NET and I missed the "Handles" syntax in your example).

        protected void Page_Init(object sender, EventArgs e)  
        {  
            rmbRadMsgBox.YesSelected += new RadMsgBox.RadMsgBox.YesSelectedEventHandler(rmbRadMsgBox_YesSelected);  
            rmbRadMsgBox.NoSelected += new RadMsgBox.RadMsgBox.NoSelectedEventHandler(rmbRadMsgBox_NoSelected);  
            rmbRadMsgBox.PromptResponse += new RadMsgBox.RadMsgBox.PromptResponseEventHandler(rmbRadMsgBox_PromptResponse);   
        } 

    Now that I have it implemented, let me just say kudos on a job well done.

    I have however come across a limitation that I want to get your insight on.

    Even though Telerik has said many times in the past that they would change the implementation, the only way to currently localize (or change the default strings) of elements in a RadWindow is through a small piece of javascript that must be declared after the Window on the page (either RadWindowManager or a RadWindow itself). This change in the strings cannot be done from codebehind.

     

        <script type="text/javascript" >  
            Telerik.Web.UI.RadWindowUtils.Localization =     
            {    
                "Close" : "Close",    
                "Minimize" : "Minimize",    
                "Maximize" : "Maximize",    
                "Reload" : "Reload",    
                "PinOn" : "Pin on",    
                "PinOff" : "Pin off",    
                "Restore" : "Restore",    
                "OK" : "Yes",    
                "Cancel" : "No"   
            };    
        </script> 

    The dilema is the same whether you are localizing OR if in my case, you need RadConfirms that are OK/Cancel and Yes/No in nature.

    I have been kicking around different possible solutions to this problem (i.e. allow the user to pass in the strings they want to use to Set/ShowConfirmation(), etc... and utilzing RegisterClientScriptBlock, but before I proceed I wanted to see if you had any insight since this is your baby. If this is not something you have interest in, please let me know and once I devise a solution I will publish it.

    Thanks,

    Jim
  9. James Legan
    James Legan avatar
    54 posts
    Member since:
    Sep 2008

    Posted 14 Apr 2009 Link to this post

    Here is a modified ShowConfirmation() to demo my theory. This allows the user to pass in a string for the OK and Cancel buttons, allowing them to localize it in codebehind.

    Public Sub ShowConfirmation(ByVal stConfirmationMessage As StringByVal Key As StringByVal PostBackOnYes As BooleanByVal PostBackOnNo As BooleanOptional ByVal Width As Integer = 300, Optional ByVal Height As Integer = 100, Optional ByVal stTitle As String = "<strong>Confirm</strong>"Optional ByVal sYesText As String = "OK"Optional ByVal sNoText As String = "Cancel")  
            ' this method exposes an active way to initiate a confirmation message from the server  
            On Error GoTo Err_ShowConfirmation  
     
            _MsgType = MsgType.ConfirmMsg   ' set for instance processing so the class understands what method it is rendering  
     
            If stConfirmationMessage = String.Empty Then 
                Exit Sub 
            End If 
            If Key = String.Empty Then 
                Exit Sub 
            End If 
     
            Dim stScript As String = _  
          "<script type=""text/javascript"">" & vbCrLf & _  
          " //<![CDATA[" & vbCrLf & vbCrLf & _  
          " Sys.Application.add_init(" & GetUniqueValue & "AppInit);" & vbCrLf & _  
          " function " & GetUniqueValue & "AppInit() {" & vbCrLf & _  
          "     Sys.Application.add_load(" & GetUniqueValue & "RunOnce);" & vbCrLf & _  
          " }" & vbCrLf & _  
          " function " & GetUniqueValue & "RunOnce() {" & vbCrLf & _  
          "   // This will only happen once per GET request to the page." & vbCrLf & _  
          "   " & GetUniqueValue & "ShowConfirmation();" & vbCrLf & _  
          "   Sys.Application.remove_load(" & GetUniqueValue & "RunOnce);" & vbCrLf & _  
          " }" & vbCrLf & _  
          " function " & GetUniqueValue & "ShowConfirmation() {" & vbCrLf & _  
          "   radconfirm('" & stConfirmationMessage.Replace("'""\'") & "', RadMsgBoxconfirmCallBackFn_" & GetUniqueValue & Key & ", " & Width & ", " & Height & ", null, '" & stTitle & "');" & _  
          " }" & vbCrLf & _  
          "//]]>" & vbCrLf & vbCrLf  
     
            If sYesText.Length > 0 Or sNoText.Length > 0 Then 
                stScript = stScript & _  
                "Telerik.Web.UI.RadWindowUtils.Localization =   " & vbCrLf & _  
                "{" & vbCrLf & _  
                "'Close' : 'Close', " & vbCrLf & _  
                "'Minimize' : 'Minimize'," & vbCrLf & _  
                "'Maximize' : 'Maximize',  " & vbCrLf & _  
                "'Reload' : 'Reload', " & vbCrLf & _  
                "'PinOn' : 'Pin on'," & vbCrLf & _  
                "'PinOff' : 'Pin off'," & vbCrLf & _  
                "'Restore' : 'Restore'," & vbCrLf & _  
                "'OK' : " & "'" & sYesText & "'" & "," & vbCrLf & _  
                "'Cancel' : " & "'" & sNoText & "'" & vbCrLf & _  
                "};" & vbCrLf & vbCrLf  
            End If 
     
            stScript = stScript & "</script>" & vbCrLf & vbCrLf  
     
     
            ' if using a script manager, register with the script manager, otherwise, add to ClientScript  
            Dim manager As RadScriptManager = RadScriptManager.GetCurrent(Page)  
            If manager IsNot Nothing AndAlso manager.IsInAsyncPostBack Then 
                RadScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "_RadMsgBox_radconfirm", stScript, False)  
            Else 
                If Not Page.ClientScript.IsStartupScriptRegistered("_RadMsgBox_radconfirm"Then 
                    Page.ClientScript.RegisterStartupScript(GetType(Page), "_RadMsgBox_radconfirm", stScript)  
                End If 
            End If 
     
     
     
     
            ' preserve the key for later use  
            _Key = Key  
     
            ' set postback options  
            _PostBackOnYes = PostBackOnYes  
            _PostBackOnNo = PostBackOnNo  
            _PostBackOnPrompt = False 
     
            ' create client handler script  
            FunctionOutputArray.Add(GenerateJavaScript())  
     
     
    Exit_ShowConfirmation:  
     
            Exit Sub 
     
    Err_ShowConfirmation:  
     
            Resume Exit_ShowConfirmation  
     
        End Sub 
  10. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 17 Apr 2009 Link to this post

    Jim

    Thanks for the feedback, yes, you need to define the events inline. I typically wire them up in VB, but your method obviously works too. Regarding localization, I like your approach! I would actually love tekerik to provide this type of API functionality for their dialogs.

    -David
  11. Cynthia Stone
    Cynthia Stone avatar
    6 posts
    Member since:
    May 2009

    Posted 22 Jun 2009 Link to this post

    I've attempted to use your MsgBox control but with no luck. In my test project, I can run thru the test page with no errors but the confirm window never pops up.

    In my aspx page:
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager> 
     <telerik:RadWindowManager id="RadWindowManager1" runat="server"></telerik:RadWindowManager>
    <RadMsgBox:RadMsgBox ID="RadMsgBox1" runat="server" OnYesSelected="RadMsgBox1_YesSelected" OnNoSelected="RadMsgBox1_NoSelected" />

    In my code behind:
    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
    ...
    If ponum <> "NULL" Then
    Dim msg1 As String = "You are attempting to change the purchase vendor on this item. Other items sharing this purchase order number will be changed as well. Do you want to proceed?"
    If vendorID <> hidOldPurchID.Value Then
    Me.RadMsgBox1.ShowConfirmation(msg1, "CONFIRM", True, True, 400, 125)
    End IF
    End IF

    Am I missing something to make the control work?
    Thanks,
    Cindy




     

     

     

     

     

     

     

     

     

     

     

     

     

  12. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 22 Jun 2009 Link to this post

    Cindy,

    I think the problem is that you need to explicity wire up the event handlers (this is an oversight, er, bug in the control
    that I need to fix).

    try,

    Protected Sub RadMsgBox1_YesSelected(sender as object, key as string) Handles RadMsgBox1.YesSelected
        ' remember that the "key" parameter holds the specific instance value

    End Sub

    Let me know if that works...

    -David
  13. Cynthia Stone
    Cynthia Stone avatar
    6 posts
    Member since:
    May 2009

    Posted 22 Jun 2009 Link to this post

    I omitted this from my original post:
     Protected Sub RadMsgBox1_YesSelected(ByVal sender As Object, ByVal Key As String) Handles RadMsgBox1.YesSelected
            On Error GoTo Err_RadMsgBox1_YesSelected
            Me.lblError.Text = "YES selected for button with key: " & Key
    Exit_RadMsgBox1_YesSelected:
            Exit Sub
    Err_RadMsgBox1_YesSelected:
            Resume Exit_RadMsgBox1_YesSelected
        End Sub
        Protected Sub RadMsgBox1_NoSelected(ByVal sender As Object, ByVal Key As String) Handles RadMsgBox1.NoSelected
            On Error GoTo Err_RadMsgBox1_NoSelected
            Select Case Key
                Case "DOSOMETHING"
                Case "DOSOMETHING2"
            End Select
            Me.lblError.Text = "NO selected for button with key: " & Key
    Exit_RadMsgBox1_NoSelected:
            Exit Sub
    Err_RadMsgBox1_NoSelected:
            Resume Exit_RadMsgBox1_NoSelected
        End Sub

    But this never gets called since the confirm popup does not display.
  14. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 23 Jun 2009 Link to this post

    Your code looks good to me. I tested and it works for me. Couple of things to try:

    1. Make sure the RadMsgBox server control definition is NOT in an ajaxified panel. That is,
    the definition should be the first thing that appears in the .aspx file under the body tag
    and NOT in a panel that is managed by the RadAjaxManager or RadAjaxManagerProxy
    2. I assume you've correctly added a reference to the server control to your project, but
    check to make sure that it's properly registered in the page:

    <%

    @ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

     

    <%

    @ Register TagPrefix="RadMsgBox" Namespace="RadMsgBox" Assembly="RadMsgBox" %>


    Good luck

     

  15. Cynthia Stone
    Cynthia Stone avatar
    6 posts
    Member since:
    May 2009

    Posted 23 Jun 2009 Link to this post

    David,

    I added this in the Page_Init and it is working great now.
     Protected Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Init
    AddHandler RadMsgBox1.NoSelected, AddressOf RadMsgBox1_NoSelected
    AddHandler RadMsgBox1.YesSelected, AddressOf RadMsgBox1_YesSelected
    End Sub

    Thanks for all your help.

     

     

     

     

  16. Scott Eoff
    Scott Eoff avatar
    14 posts
    Member since:
    Jun 2005

    Posted 09 Jul 2009 Link to this post

    David,

    I'm using your class..... and I must say  "AWESOME" and I agree this needs to be implemented for us. I'm have a little issue and I wanted to get your help. When I do a ShowConfirmation and change the Yes & No button text. The very next time I call a ShowAlert the button text is the same as when I called the Show Confirm and it didn't reset back to "OK". What can I do to fix this?

    Also... I'm using ajax so it maybe something with that, meaning keep the state I would assume. Any thoughts?

    Thanks for the code,
    Scott
  17. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 09 Jul 2009 Link to this post

    Scott,

    I'm pleased you like the library. I assume when you are saying that you change the Yes/No text for the button
    that you are referring to the radconfirm label properties for the window. One of the contributors to this thread did
    post some localization code back in April to modify the Yes/No labels. However, it wasn't from me and I haven't
    tested or incorporated it into my library. Sorry!
  18. R
    R avatar
    8 posts
    Member since:
    Apr 2008

    Posted 08 Sep 2009 Link to this post

    Hi David.

    I'm having some problems.

    Whe trying Me.RadMsgBox1.ShowAlert("teste"), ie gives me an error. Whe I debug the javascript :

    /* END Telerik.Web.UI.Window.RadWindow.js */<br>/* START Telerik.Web.UI.Window.RadWindowManager.js */<br>Type.registerNamespace("Telerik.Web.UI");<br>Type.registerNamespace("Telerik.Web.UI.WindowManager");<br>function GetRadWindowManager(){return Telerik.Web.UI.WindowManager.Manager<br><b>}window.radalert=function(f,c,a,b){var e=GetRadWindowManager();</b><br>var d=e._getStandardPopup("alert",f);<br>if(typeof(b)!="undefined"){d.set_title(b)<br>}d.setSize(c?c:280,a?a:200);<br>d.show();<br>d.center();<br>return d<br>}; 


    it says that e is null or not an object in e._getStandardPopup. Can you help me? thanks

    I found the problem. It was because of the form tags. Thanks for this great user control.
  19. R
    R avatar
    8 posts
    Member since:
    Apr 2008

    Posted 08 Sep 2009 Link to this post

    It possible to pause the thread til i click ok in the showAlert window?

    Thanks
  20. Leadeng Leadeng
    Leadeng Leadeng avatar
    21 posts
    Member since:
    Mar 2008

    Posted 06 Oct 2009 Link to this post

    Hi

    I am also having the same issue. 
    Getting error on the following line when using radalert
    var d=e._getStandardPopup("alert",f);\n


    Can you let me know how was this resolved?

    Thanks
    Manish
  21. Adam
    Adam avatar
    39 posts
    Member since:
    Jun 2009

    Posted 19 Nov 2009 Link to this post

    Anyone do a C# version?

  22. James Legan
    James Legan avatar
    54 posts
    Member since:
    Sep 2008

    Posted 20 Nov 2009 Link to this post

    I never got around to rewriting it in C# for my needs, but I did tweak it and use it in all of my C# projects pretty much as is.
  23. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 20 Nov 2009 Link to this post

    Hello Adam,

    You can use our converter at http://www.codechanger.com to convert VB to C# and vice versa.


    All the best,
    Georgi Tunev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  24. doug
    doug avatar
    4 posts
    Member since:
    May 2009

    Posted 01 Dec 2009 Link to this post

    Any chance of a C# version ?
  25. Chuck Harrington
    Chuck Harrington avatar
    28 posts
    Member since:
    Dec 2009

    Posted 03 Feb 2010 Link to this post

    Protected Sub cmdPost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPost.Click  
            Dim strPostMsg As String = "You are about to Post this Notice are you sure you want to do this?" 
     
            rmbPostConfirm.ShowConfirmation(strPostMsg, "CONFIRM", True, True)  
     
        End Sub 

    Hi David,

    I am attempting to use your server-side control for radconfirm but have run into a situation that I cannot figure out.  The problem is when I execute the showConfirmation method the code for the YesSelected is run 3 times.  Within this method, I am updating a database table field and inserting some data into another table's fields.  It runs the entire method 3 times.  I have looked through your class and my code and am stumped as to why this is happening.  My handlers are all in the proper place, the script manager and rad window manager are properly placed.  My call to the method is included.  I end up with 2 unnecessary updates to the same field in one database table and 3 duplicate records in the second field.  Any ideas?

     

    Thanks

  26. David Eisner
    David Eisner avatar
    16 posts
    Member since:
    Jun 2007

    Posted 03 Feb 2010 Link to this post

    Hi chuck,

    Hard to tell from your post/snippet. Could be several things including how you are handling the postback, declaring the control, etc.

    Check the following:

    1. Page_Load handler only handling MyBase.PageLoad and not additional events (e.g. Me.PageLoad)
    2. In the YesSelected handler make sure you are Select'ing on the "Key" argument and only responding the correct passed in Key
    3. Try not setting "True" for the No selected option

    Try debugging the source for the control and see who/what us raising the YesSelected event each time

    Good luck
  27. Chuck Harrington
    Chuck Harrington avatar
    28 posts
    Member since:
    Dec 2009

    Posted 03 Feb 2010 Link to this post

    Hi David

    thanks for your quick response, but no sooner than I posted, I began experimenting with removing possible duplication and that solved my problem.  Being new to working with telerik, I had duplicated several events.  By removing the onYesSelected and onNoSelected from the RadMsgBox control that elimated one duplicated record from the table.  I have a routine for each in the code behind module that is tied to the RadMsgBox control's ID property.  The ShowConfirmation method is called from a button Click event. The second duplicated record was caused by me mistakenly adding a Page_Init event that added another set of handlers for the RadMsgBox control's YesSelected and NoSelected properties.  By removing this event, I eliminated the final duplication.  The control works great now and as expected.   Thanks again for developing it as I like it much better than client-side controls.  Plus I learned something by reading through your control's code.
  28. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 04 Feb 2010 Link to this post

    Hi guys,

    David, I just wanted to express our gratitude for your continuous support for your Code Library - we really appreciate your efforts and commitment.

    Your points have been updated (again :) )



    All the best,
    Georgi Tunev
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  29. Alberto
    Alberto avatar
    7 posts
    Member since:
    Jul 2008

    Posted 09 Feb 2010 Link to this post

    Does anyone have the RadMsgBox using Q3 2009 release of Telerik.Web.UI?

    David, This DLL only works with Q2 2009 for Telerik.Web.UI., is that right?

    Regards,

    J.A.
  30. Eric Moore
    Eric Moore avatar
    25 posts
    Member since:
    Mar 2010

    Posted 06 Apr 2010 Link to this post

    Thanks a lot. Great tool!
Back to Top