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

Forums / Developing with Sitefinity / Set a .ascx properties programmatically

Set a .ascx properties programmatically

4 posts, 0 answered
  1. Christopher
    Christopher avatar
    22 posts
    Registered:
    09 Jun 2008
    29 Nov 2008
    Link to this post
    How can you set a user control's properties programmatically? I see how you can set a generic content "content" property with a specified method, but what about a .ascx? It seems like you have some interfaces and classes for example Telerik.cms.ICmsControlProperty. How can i use this in the following circumstance:

    1. Create and save a page programatically
    2. Place a user control on the page using oCMSPage.Staged.AddControl("Control URL")
    3. ... Here i need to set the properties....
    4. oCMSPage.Publish

    If you can help it would be greatly appreciated.

    Thanks,
    Chris
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Dec 2008
    Link to this post
    Hi Christopher,

    Here is an example that demonstrates how to set  control properties programmatically. I am using the TextBox and Button controls to set the Text of the corresponding Label control (MyControl.ascx)

    public partial class CustomControls_CtrlProperties : System.Web.UI.UserControl 
        protected void Page_Load(object sender, EventArgs e) 
        { 
        } 
     
        private void SetMyControlText(string text) 
        {  
            //new instance of the CmsManager 
            CmsManager manager = new CmsManager(); 
            IList allPages = manager.GetPages(); 
            foreach (ICmsPage page in allPages) 
            { 
                if (page.PageType != CmsPageType.Normal) 
                    continue
     
                ICmsPage currentPage = (ICmsPage)manager.GetPage(page.ID, true); 
                IStagedPage staged = currentPage.Staged; 
                if (staged.Locked) 
                    continue
                //check out 
                staged = currentPage.Staged.CheckOut(); 
     
                foreach (ICmsWebControl cmsControl in staged.Controls) 
                { 
                    if (cmsControl.TypeName.Equals("~/CustomControls/MyControl.ascx")) 
                    { 
     
                        ICmsWebControl editedControl = manager.GetControl(cmsControl.ID, true); 
                        CustomControls_MyControl myControl = editedControl.LoadControl(this.Page) as CustomControls_MyControl; 
                        if (myControl != null
                        { 
                            myControl.Text = text; 
                            //save the control 
                            editedControl.SetControl(myControl); 
                            manager.SaveControl(editedControl); 
                        } 
                    } 
                } 
                //check in 
                staged = manager.GetPage(page.ID, true).GetVersion(staged.Version); 
                staged.CheckIn(); 
            } 
        } 
        //setting the text of the label 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            SetMyControlText(TextBox1.Text); 
        } 
     


    and here is what I have in the cs of the MyControl.ascx

    public partial class CustomControls_MyControl : System.Web.UI.UserControl 
        protected void Page_Load(object sender, EventArgs e) 
        { 
        } 
     
        [Browsable(true)] 
        public string Text 
        { 
            //add public properties 
            get  
            {  
                return lbl.Text;  
            } 
            set  
            {  
                lbl.Text = value;  
            } 
        } 
     

    I hope this helps.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Christopher
    Christopher avatar
    22 posts
    Registered:
    09 Jun 2008
    01 Dec 2008
    Link to this post
    Hey Ivan,

    Thanks for the response. In the example below CustomControls_MyControl would have to exist inside CustomControls_CtrlProperties in order to be able to declare a variable of type MyControl. My code is going to be from a web service so I don't think this method would be possible. Ultimately what I'm trying to do is set the values in the table dbo.sf_CmsControlProperty for particular controls. I can get a reference to a particular control on a page using the code below from the developers guide. Once I have a reference to the control can I set the properties?

    Thanks again,
    Chris

    // find the sitemap node of the current page 
    Telerik.Cms.Web.CmsSiteMapNode currentNode = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.CurrentNode; 
    // get the CmsPage associated with the current node 
    Telerik.Cms.ICmsPage currentPage = currentNode.CmsPage; 
    // we are going to use Controls collection of a staged version of a page to 
    // access all controls of current page 
    IList<Telerik.Cms.ICmsWebControl> pageControls = currentPage.Staged.Controls; 
    foreach (Telerik.Cms.ICmsWebControl webCtrl in pageControls) 
     // let's write out some basic info on each control of this page 
     string ctrlInfo = String.Concat("Control ID = ", webCtrl.ID, 
                                     ", Control type = ", webCtrl.ControlType, 
                                     ", Container ID = ", webCtrl.ContainerID, 
                                     ", Is UserControl = ", webCtrl.IsAscx); 
     Response.Write(ctrlInfo + "<br />"); 
    }  

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Dec 2008
    Link to this post
    Hello Christopher,

    Thank you for getting back to me.

    We do not have API that illustrates setting the properties directly without creating an instance. There are two ways that you can use to achieve the desired functionality:

    1. Set the properties via reflection.
    2. Creating interfaces for your control and then cast it to the corresponding interface.

    create:

    public interface IMyControl 
        string Text { getset; } 

    cast:

    IMyControl myControl = editedControl.LoadControl() as IMyControl; 
     
    I hope this helps.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
4 posts, 0 answered