More in this section

Forums / Developing with Sitefinity / Using the Browse Features of SiteFinity in a user control

Using the Browse Features of SiteFinity in a user control

8 posts, 0 answered
  1. Damian
    Damian avatar
    42 posts
    Registered:
    25 Mar 2008
    15 Sep 2008
    Link to this post
    Hello.

    I am developing a User Control for within a SiteFinity Page.

    I need to give the user a button that when clicked they can select a page from the current sitefinity site, or select a document (either within a library or just on the server) to link to.

    How can I achieve this ?  Can it be done using the existing controls in Sitefinity such as the NavigateUrl box.

    Thanks

    Damian
  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    15 Sep 2008
    Link to this post
    Hello Damian,

    Here is a sample implementation of the desired functionality:

    .cs
    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using Telerik.Cms.Web.UI; 
    using System.ComponentModel; 
    using Telerik.Web.UI; 
     
    namespace MyLibrary 
        public class DocumentSelector : CompositeControl, ITextControl 
        { 
            [Browsable(false)] 
            public TextBox DownloadTextBox 
            { 
                get 
                { 
                    return this.FindControl("url"as TextBox; 
                } 
            } 
     
            protected override void OnPreRender(EventArgs e) 
            { 
                base.OnPreRender(e); 
                if (this.Page != null
                { 
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callback"this.GetScript(), true); 
                } 
            } 
     
            protected override void CreateChildControls() 
            { 
                this.Controls.Clear(); 
     
                if (this.itemTemplate == null
                { 
                    this.itemTemplate = new DefaultItemTemplate(); 
                } 
                this.itemTemplate.InstantiateIn(this); 
            } 
     
     
            private string GetScript() 
            { 
                string result = "function CallBackFunction(radWindow, returnValue)" + 
                        "{" + 
                                "var oArea = document.getElementById('" + DownloadTextBox.ClientID + "');" + 
                                "if (returnValue) oArea.value = returnValue.src;" + 
                        "}"
     
                return result; 
            } 
     
            private ITemplate itemTemplate; 
     
            private class DefaultItemTemplate : ITemplate 
            { 
                public void InstantiateIn(Control container) 
                { 
                    TextBox txt = new TextBox(); 
                    txt.ID = "url"
                    container.Controls.Add(txt); 
     
                    LinkButton btn = new LinkButton(); 
                    btn.ID = "picker"
                    btn.Text = "Select"
                    container.Controls.Add(btn); 
     
                    RadWindowManager manager = new RadWindowManager(); 
                    manager.ID = "RadWindowManager"
                    manager.VisibleOnPageLoad = false
                    container.Controls.Add(manager); 
     
                    RadWindow window = new RadWindow(); 
                    window.ID = "RadWindow1"
                    window.OpenerElementID = btn.ClientID; 
                    window.NavigateUrl = "~/Sitefinity/UserControls/Dialogs/PagesSelector.aspx"
                    window.Width = Unit.Point(750); 
                    window.Height = Unit.Point(515); 
                    window.VisibleOnPageLoad = false
                    window.ClientCallBackFunction = "CallBackFunction"
                    manager.Windows.Add(window); 
                } 
            } 
            #region ITextControl Members 
     
            string ITextControl.Text 
            { 
                get 
                { 
                    if (this.DownloadTextBox != null
                        return DownloadTextBox.Text; 
     
                    return string.Empty; 
                } 
                set 
                { 
                    DownloadTextBox.Text = value; 
                } 
            } 
            #endregion 
        } 
    }   
     

    For your convenience I have attached the full example. To test it you need just to put the file in App_Code folder and register the control in your user control like this:

    .ascx
    <%@ Register Assembly="App_Code" Namespace="MyLibrary" TagPrefix="cc" %> 

    Do let us know if you need any further assistance.

    Greetings,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Damian
    Damian avatar
    42 posts
    Registered:
    25 Mar 2008
    15 Sep 2008
    Link to this post

    Thanks Pepi,

    that's great.  Just 2 points.

    1) The .rar file you attached seems to be empty

    2) Can this be configured to select a document from a library in the Images & Documents module ?


    Damian
  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    16 Sep 2008
    Link to this post
    Hi Damian,

    1. The .rar contains DocumentSelector.cs file. Could you save the attachment locally and then unzip it?
    2. The control can be modified to select items from a library in the Images&Documents module. But in this case you will lose the option to select pages from the Sitemap. To allow selecting documents from a library you need to make the following changes in the DocumentSelector class:
     - Modify GetScript method like this:

    private string GetScript() 
            { 
                string result = "function CallBackFunction(radWindow, returnValue)" + 
                        "{" + 
                                "var oArea = document.getElementById('" + DownloadTextBox.ClientID + "');" + 
                                "if (returnValue) oArea.value = returnValue;" + 
                        "}"
     
                return result; 
            } 

     - Modify InstantiateIn method of the DefaultItemTemplate like this:

    private class DefaultItemTemplate : ITemplate 
            { 
                public void InstantiateIn(Control container) 
                { 
                    ... 
                    window.NavigateUrl = "~/Sitefinity/UserControls/Dialogs/DocumentEditorDialog.aspx"
                    ... 
                } 
            } 

    Hope this helps.

    Greetings,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. JohnGassman
    JohnGassman avatar
    18 posts
    Registered:
    03 Nov 2006
    27 Oct 2008
    Link to this post
    Hello,

    I am trying to use the code provided in the DocumentSelector.rar example code, and it does not appear to be working with Sitefinity 3.5. If I add a new metafield and try to use a control based off of the DocumentSelector code on the Insert/Edit pages, *none* of the metafields update - even ones that work just fine without the selector control on the page.

    The steps to reproduce:

    Add a test metafield named "ContentPage" to Generic Content:
    <metaFields> 
    ... 
        <add key="Generic_Content.ContentPage" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue=""/> 
    ... 
    </metaFields> 
     

    Created a control App_Code\JDNA\Web\UI\WebControls\LessonPageSelector.cs, based on the DocumentSelector Code:
    1using System; 
    2using System.Data; 
    3using System.Configuration; 
    4using System.Web; 
    5using System.Web.Security; 
    6using System.Web.UI; 
    7using System.Web.UI.HtmlControls; 
    8using System.Web.UI.WebControls; 
    9using System.Web.UI.WebControls.WebParts; 
    10using System.ComponentModel; 
    11 
    12using Telerik.Cms.Web.UI; 
    13using Telerik.Web.UI; 
    14 
    15namespace JDNA.Web.UI.WebControls 
    16
    17    public class LessonPageSelector : CompositeControl, ITextControl 
    18    { 
    19        [Browsable(false)] 
    20        public TextBox PageTextBox 
    21        { 
    22            get 
    23            { 
    24                return this.FindControl("page") as TextBox; 
    25            } 
    26        } 
    27 
    28        protected override void OnPreRender(EventArgs e) 
    29        { 
    30            base.OnPreRender(e); 
    31            if (this.Page != null) 
    32            { 
    33                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callback", this.GetScript(), true); 
    34            } 
    35        } 
    36 
    37        protected override void CreateChildControls() 
    38        { 
    39            this.Controls.Clear(); 
    40 
    41            if (this.itemTemplate == null) 
    42            { 
    43                this.itemTemplate = new DefaultItemTemplate(); 
    44            } 
    45            this.itemTemplate.InstantiateIn(this); 
    46        } 
    47 
    48 
    49        private string GetScript() 
    50        { 
    51            string result = "function LessonPageCallBackFunction(radWindow, returnValue)" + 
    52                    "{" + 
    53                            "var oArea = document.getElementById('" + PageTextBox.ClientID + "');" + 
    54                            "if (returnValue) oArea.value = returnValue.src;" + 
    55                    "}"; 
    56 
    57            return result; 
    58        } 
    59 
    60        private ITemplate itemTemplate; 
    61 
    62        private class DefaultItemTemplate : ITemplate 
    63        { 
    64            public void InstantiateIn(Control container) 
    65            { 
    66                TextBox txt = new TextBox(); 
    67                txt.ID = "page"
    68                container.Controls.Add(txt); 
    69 
    70                LinkButton btn = new LinkButton(); 
    71                btn.ID = "picker"
    72                btn.Text = "Select"
    73                container.Controls.Add(btn); 
    74 
    75                RadWindowManager manager = new RadWindowManager(); 
    76                manager.ID = "RadWindowManager"
    77                manager.VisibleOnPageLoad = false
    78                container.Controls.Add(manager); 
    79 
    80                RadWindow window = new RadWindow(); 
    81                window.ID = "RadWindow1"
    82                window.OpenerElementID = btn.ClientID; 
    83                window.NavigateUrl = "~/Sitefinity/UserControls/Dialogs/PagesSelector.aspx"
    84                window.Width = Unit.Point(750); 
    85                window.Height = Unit.Point(515); 
    86                window.VisibleOnPageLoad = false
    87                window.ClientCallBackFunction = "LessonPageCallBackFunction"
    88                manager.Windows.Add(window); 
    89             
    90                container.Controls.Add(btn); 
    91            } 
    92        } 
    93 
    94        #region ITextControl Members 
    95 
    96        string ITextControl.Text 
    97        { 
    98            get 
    99            { 
    100                if (this.PageTextBox != null) 
    101                    return PageTextBox.Text; 
    102 
    103                return string.Empty; 
    104            } 
    105            set 
    106            { 
    107                PageTextBox.Text = value
    108            } 
    109        } 
    110 
    111        #endregion 
    112    } 
    113}   
    114 

    Update the Generic Content ControlPanelInsert.ascx file to add a control for the new metafield in the 'sfGCn:ContentMetaFields' container, along with a new Register statement for the new control:
    <%@ Register TagPrefix="jdna" Namespace="JDNA.Web.UI.WebControls" Assembly="App_Code" %> 
     

    <li> 
        <asp:Label ID="lblPage" runat="server" Text="Content Page" AssociatedControlID="ContentPage"></asp:Label> 
        <jdna:LessonPageSelector ID="ContentPage" runat="server" /> 
    </li> 
     

    which I added immediately below the existing Author field.

    Then, when I create a new Generic Content item, the new control appears, and seems to function correctly, but when I save the new item, none of the metafields are updated - only the base Name and Content fields.

    If I comment out line 82 in LessonPageSelector.cs:
    //window.OpenerElementID = btn.ClientID; 



    then the save works - i.e. updates all metafields, but the selector no longer works (no dialog pops up).

    Am I missing something? Does that example code not work in 3.5?  I am in desperate need to get a page and document dialog control working in a custom module I am building.

    I'm guessing some exception is being thrown and swallowed deep in Sitefinity code somewhere, and I haven't been able to track down what the real issue is with my code.

    Thanks for any input!

    Regards,
    John
  6. Damian
    Damian avatar
    42 posts
    Registered:
    25 Mar 2008
    29 Oct 2008
    Link to this post
    All,

    I have created a DocumentSelector and an ImageSelector based on the code here, that work in the previous version of sitefinity.

    Now I have updated to v3.5 the ImageSelector returns undefined and the document selector returns [object Object].

    does anyone know why this might be?  Also how do you upload files to the forums so I can show you.  Otherwise I will paste the entire classes here.

    Thanks

    Damian
  7. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    30 Oct 2008
    Link to this post
    Hi John,

    To solve the issue please set RadWindow OpenerElementID property in OnLoad method. Here is the full implementation:

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.ComponentModel;  
    using Telerik.Cms.Web.UI;  
    using Telerik.Web.UI;  
    namespace JDNA.Web.UI.WebControls  
    {  
        public class LessonPageSelector : CompositeControl, ITextControl  
        {  
            [Browsable(false)]  
            public TextBox PageTextBox  
            {  
                get  
                {  
                    return this.FindControl("page"as TextBox;  
                }  
            } 
     
            protected override void OnPreRender(EventArgs e)  
            { 
                base.OnPreRender(e); 
     
                if (this.Page != null
                { 
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callback"this.GetScript(), true); 
                } 
            } 
     
            protected override void OnLoad(EventArgs e) 
            { 
                base.OnLoad(e); 
                 
                RadWindow window = ((RadWindowManager)this.FindControl("RadWindowManager1")).Windows[0]; 
                LinkButton btn = this.FindControl("picker"as LinkButton; 
                 
                if(window != null && btn != null
                    window.OpenerElementID = btn.ClientID; 
            } 
     
            protected override void CreateChildControls()  
            {  
                this.Controls.Clear(); 
     
                if (this.itemTemplate == null
                { 
                    this.itemTemplate = new DefaultItemTemplate(); 
                } 
                this.itemTemplate.InstantiateIn(this); 
            }  
      
            private string GetScript()  
            {  
                string result = "function LessonPageCallBackFunction(radWindow, returnValue)" +  
                        "{" +  
                                "var oArea = document.getElementById('" + PageTextBox.ClientID + "');" +  
                                "if (returnValue) oArea.value = returnValue.src;" +  
                        "}";  
      
                return result;  
            } 
     
            private ITemplate itemTemplate; 
     
            private class DefaultItemTemplate : ITemplate 
            { 
                public void InstantiateIn(Control container) 
                { 
                    TextBox txt = new TextBox(); 
                    txt.ID = "page"
                    container.Controls.Add(txt); 
     
                    LinkButton btn = new LinkButton(); 
                    btn.ID = "picker"
                    btn.Text = "Select"
                    container.Controls.Add(btn); 
     
                    RadWindowManager manager = new RadWindowManager(); 
                    manager.ID = "RadWindowManager1"
                    manager.VisibleOnPageLoad = false
                    container.Controls.Add(manager); 
     
                    RadWindow window = new RadWindow(); 
                    window.ID = "RadWindow1"
                    window.NavigateUrl = "~/Sitefinity/UserControls/Dialogs/PagesSelector.aspx"
                    window.Width = Unit.Point(750); 
                    window.Height = Unit.Point(515); 
                    window.VisibleOnPageLoad = false
                    window.ClientCallBackFunction = "LessonPageCallBackFunction"
                    manager.Windows.Add(window); 
                } 
            }  
     
            #region ITextControl Members  
      
            string ITextControl.Text  
            {  
                get  
                {  
                    if (this.PageTextBox != null)  
                        return PageTextBox.Text;  
      
                    return string.Empty;  
                }  
                set  
                {  
                    PageTextBox.Text = value;  
                }  
            }  
     
            #endregion  
        }  
    }  

    Do let us know if this helps.

    Regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. JGassman
    JGassman avatar
    6 posts
    Registered:
    11 Jul 2006
    30 Oct 2008
    Link to this post
    Thank you Pepi!

    I found another example implemented as a user control, which works and is actually better suited to what I need.  Though, I can't believe I didn't think to try the OnLoad trick ;).

    Thanks again for your help. You guys provide excellent support!

    Regards,
    John
Register for webinar
8 posts, 0 answered