1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Fixes for using multiple ButtonSelector controls on a page

Fixes for using multiple ButtonSelector controls on a page

5 posts, 0 answered
  1. Daniel Flippance
    Daniel Flippance avatar
    2 posts
    Registered:
    08 Jul 2008
    21 Oct 2008
    Link to this post
    With regard to this Knowledge Base article:
    http://www.sitefinity.com/support/knowledge-base/kb-article/b1154K-baba-b1154T-cgh.aspx

    The following code fixes the problems:
    - Can't put an Image Library ButtonSelector and a Page ButtonSelector on the same page at the same time.
    - When you put multiple ButtonSelectors on a page at the same time, the selected value is always inserted into one of the AssociatedControls, not necessarily the correct one.

    Paste this code into:
    \Sitefinity\Admin\ControlTemplates\Libraries\Dialogs\ButtonSelector.ascx

    This code could be extended to support multiple AssociatedControls.

    Cheers,
    Daniel

    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
     
    <script type="text/javascript">  
        var selectedItem = null;  
     
        //CHANGE  
        function openDialog(clientId, assignedControlIds) {  
            var window = $find(clientId);  
            assignedControls = assignedControlIds.split(',');  
            window.show();  
            //window.set_Status("");  
            //alert(window.parent);  
        }  
        //ENDCHANGE  
     
        function OnClientClose(sender, eventArgs) {  
            if (sender.argument) {  
                var title = '';  
                var altText = '';  
                var src = '';  
                var width;  
                var height;  
                var unresolved;  
     
                //CHANGE      
                sendersender.argument.src = sender.argument.src.toLowerCase().replace("/myrootfolder/", "~/");  
                //ENDCHANGE   
     
                if (sender.argument.title)  
                    title = sender.argument.title;  
                if (sender.argument.altText)  
                    altText = sender.argument.altText;  
                if (sender.argument.src)  
                    src = sender.argument.src;  
                if (sender.argument.width && sender.argument.height) {  
                    width = sender.argument.width;  
                    height = sender.argument.height;  
                }  
                if (sender.argument.unresolved)  
                    unresolved = sender.argument.unresolved;  
                      
                selectedItem = new SelectedItem(src, altText, title, width, height, unresolved);  
     
                for (i = 0; i < assignedControls.length; i++) {  
                    if (assignedControls[i] != '') {  
                        var element = document.getElementById(assignedControls[i]);  
                        if (element.src)  
                            if (!(/selectedImage/.test(element.id)) && !(/uploadedImage/.test(element.id)))  
                            element.src = sender.argument.src;  
                        else  
                            element.src = sender.argument.src + '?width=300&height=300&decreaseOnly=true';  
                        else if (typeof (element.value) != 'undefined' && element.value != null)  
                            element.value = sender.argument.src;  
                        else if (typeof (element.href) != 'undefined')  
                            element.href = sender.argument.src;  
                        else if (typeof (element.innerHTML) != 'undefined')  
                            element.innerHTML = sender.argument.src;  
                    }  
                }  
            }  
        }  
     
        function SelectedItem(src, altText, title, width, height, unresolved) {  
            this.src = src;  
            this.altText = altText;  
            this.title = title;  
            this.width = width;  
            this.height = height;  
            this.unresolved = unresolved;  
     
            //CHANGE      
            //beforeInsertLink(src, altText, title, width, height);  
            if (typeof(beforeInsertLink) != 'undefined')  
                beforeInsertLink(src, altText, title, width, height);    
            //ENDCHANGE   
        }  
    </script> 
     
    <script runat="server" language="vbscript">  
        'CHANGE  
        Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)  
            Dim buttonSelector As Telerik.Libraries.WebControls.ButtonSelector = CType(Parent.Parent, Telerik.Libraries.WebControls.ButtonSelector)  
            Dim associatedControlId As String = buttonSelector.Parent.FindControl(buttonSelector.AssociatedControls).ClientID  
            selectBtn.Attributes.Add("onclick", "openDialog('" + dialogWindow.ClientID + "', '" + associatedControlId + "');")  
        End Sub  
        'ENDCHANGE  
    </script> 
     
    <telerik:RadWindow OnClientClose="OnClientClose" VisibleStatusbar="false" Width="632" Height="470" runat="server" id="dialogWindow" Behaviors="close" EnableEmbeddedSkins="False" Skin="Sitefinity"></telerik:RadWindow> 
    <asp:HyperLink NavigateUrl="javascript:void(0);" runat="server" ID="selectBtn" Text="<%$Resources:Select %>"></asp:HyperLink> 

  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    21 Oct 2008
    Link to this post
    Hi Daniel,

    Thanks a lot for sharing your code sample. As a note of gratitude your Telerik points have been updated.

    Best wishes,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. OC
    OC avatar
    129 posts
    Registered:
    17 Nov 2004
    07 Dec 2008
    Link to this post
    Typo in this code!

    Line:
     sendersender.argument.src = sender.argument.src.toLowerCase().replace("/myrootfolder/", "~/"); 

    Really should be:
    sender.argument.src = sender.argument.src.toLowerCase().replace("/myrootfolder/", "~/");   
  4. OC
    OC avatar
    129 posts
    Registered:
    17 Nov 2004
    06 Sep 2009
    Link to this post
    This causes trouble when upgrading to 3.7. You will not be able to select images several places in Sitefinity when this changes are done.

    To avoid this problem just add a Try, Catch and Try End inside the VBScript Page_Load function. This makes it work when it should, and disregarded when not.

    AND TO THE SITEFINITY-TEAM! This is MUCH wanted functionality that should be standard. Please implement it :-)

    OC
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    06 Sep 2009
    Link to this post
    Hello Mutantmannen,

    Actually our team has a similar fix that should work under 3.7

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LibrarySelector.ascx.cs" Inherits="UserControls_Libraries_LibrarySelector" %> 
     
    <script type="text/javascript"
        var selectedItem2 = null
     
        function openDialog2() { 
            var window = $find("<%= dialogWindow.ClientID %>"); 
            window.show(); 
            //window.set_Status(""); 
            //alert(window.parent); 
     
        } 
     
        function OnClientClose2(sender, eventArgs) { 
            if (sender.argument) { 
                var title = ''
                var altText = ''
                var src = ''
                var width; 
                var height; 
                var unresolved; 
     
                if (sender.argument.title) 
                    title = sender.argument.title; 
                if (sender.argument.altText) 
                    altText = sender.argument.altText; 
                if (sender.argument.src) 
                    src = sender.argument.src; 
                if (sender.argument.width && sender.argument.height) { 
                    width = sender.argument.width; 
                    height = sender.argument.height; 
                } 
                if (sender.argument.unresolved){ 
                    unresolved = sender.argument.unresolved; 
                } 
                else if (sender.argument.attributes["sfref"] != undefined) { 
                    unresolved = sender.argument.attributes["sfref"].value; 
                } 
     
                selectedItem2 = new selectedItem2(src, altText, title, width, height, unresolved); 
     
                if (typeof (sfUseResolvedUrl) == 'undefined') { 
                    sfUseResolvedUrl = true
                } 
     
                for (i = 0; i < assignedControls2.length; i++) { 
                    if (assignedControls2[i] != '') { 
                        var element = document.getElementById(assignedControls2[i]); 
                        if (element.src) 
                            if (!(/selectedImage/.test(element.id)) && !(/uploadedImage/.test(element.id))) 
                            element.src = sender.argument.src; 
                        else 
                            element.src = sender.argument.src + '?width=300&height=300&decreaseOnly=true'; 
                        else if (typeof (element.value) != 'undefined' && element.value != null) { 
                            if ( !sfUseResolvedUrl && (/selectedValue/.test(element.id))) { 
                                element.value = unresolved;              
                            } 
                            else { 
                                element.value = unresolved
                            } 
                        } 
                        else if (typeof (element.href) != 'undefined') 
                            element.href = sender.argument.src; 
                        else if (typeof (element.innerHTML) != 'undefined') 
                            element.innerHTML = sender.argument.src; 
                    } 
                } 
            } 
        } 
     
        function selectedItem2(src, altText, title, width, height, unresolved) { 
            this.src = src; 
            this.altText = altText; 
            this.title = title; 
            this.width = width; 
            this.height = height; 
            this.unresolved = unresolved; 
            if(typeof(beforeInsertLink) != 'undefined') { 
                beforeInsertLink(src, altText, title, width, height); 
            } 
        } 
    </script> 
     
    <telerik:RadWindow OnClientClose="OnClientClose2" VisibleStatusbar="false" Width="750" Height="600" runat="server" id="dialogWindow" Behaviors="close" EnableEmbeddedSkins="False" Skin="Sitefinity"></telerik:RadWindow> 
    <asp:HyperLink NavigateUrl="javascript:void(0);" onclick="openDialog2();" runat="server" ID="selectBtn" Text="Select"></asp:HyperLink> 
     



    ublic partial class UserControls_Libraries_LibrarySelector : System.Web.UI.UserControl 
     
        public string WindowNavigateUrl 
        { 
            get 
            { 
                object o = this.ViewState["WindowNavigateUrl"]; 
                if (o == null
                    return Page.ResolveUrl("~/Sitefinity/UserControls/Dialogs/ImageDialog.aspx"); 
                return (string)o; 
            } 
            set 
            { 
                this.ViewState["WindowNavigateUrl"] = value; 
            } 
        } 
     
        public string AssociatedControls 
        { 
            get 
            { 
                object o = this.ViewState["AssociatedControls"]; 
                if (o == null
                    return String.Empty; 
                return (string)o; 
            } 
            set 
            { 
                this.ViewState["AssociatedControls"] = value; 
            } 
        } 
     
        protected override void OnPreRender(EventArgs e) 
        { 
            base.OnPreRender(e); 
     
            dialogWindow.NavigateUrl = this.WindowNavigateUrl; 
     
            if (!String.IsNullOrEmpty(this.AssociatedControls)) 
            { 
                string[] controlIDs = this.AssociatedControls.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); 
                StringBuilder arrayDeclaration = new StringBuilder("var assignedControls2 = new Array("); 
                for (int i = 0; i < controlIDs.Length; i++) 
                { 
                    Control ctrl = this.NamingContainer.FindControl(controlIDs[i].Trim()); 
                    if (ctrl != null
                    { 
                        if (i == 0) 
                            arrayDeclaration.Append("'" + ctrl.ClientID + "'"); 
                        else 
                            arrayDeclaration.Append(", '" + ctrl.ClientID + "'"); 
                    } 
                } 
                arrayDeclaration.Append(");"); 
     
                if (!this.Page.ClientScript.IsClientScriptBlockRegistered("selectedValue2")) 
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "selectedValue2"
                        arrayDeclaration.ToString(), 
                        true); 
            } 
        } 
     
     


    All the best,
    Ivan Dimitrov
    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.
Register for webinar
5 posts, 0 answered