More in this section

Forums / Developing with Sitefinity / Adding properties to Controls

Adding properties to Controls

4 posts, 0 answered
  1. Fernando
    Fernando avatar
    31 posts
    Registered:
    08 Jun 2009
    05 Mar 2010
    Link to this post
    Hi,

    I'm trying to add some properties to controls, but I'm having some problems.

    I get the control as a CmsWebControl, and then I try to add the properties.

    I've tried using Properties.Add(property), Properties_static.Add(property), PropertiesToPersist.Add(property.Name), and then I've saved using a CmsManager, but the changes are not saved.

    CmsManager manager = new CmsManager();
    CmsWebControl webControl = manager.GetControl(new Guid("321b652b-1ff2-446b-a363-2c501cfa5850"), true) as CmsWebControl;
      
    CmsControlProperty property = new CmsControlProperty();
    property.ControlID = webControl.ID;
    property.Name = "Banner";
    property.Value = "ContactUs";
      
    webControl.Properties.Add(property); //here I tried that other ways, but no one worked
      
    manager.SaveControl(webControl);

    If I check the webControl.Properties, my properties are there, but when I get the control again using the CmsManager, they are not there.

    Is there a right way to add Properties to a control, but it needs to be a way that works for all types of control (Sitefinity controls, ASP controls, User Controls)?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Mar 2010
    Link to this post
    Hi Fernando,

    You need to implement the public/private properties in the control you have got. It is not possible to add properties in the way you are trying to do it.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Fernando
    Fernando avatar
    31 posts
    Registered:
    08 Jun 2009
    05 Mar 2010
    Link to this post
    Hi Ivan,

    So if I add a Control using this:

    CmsManager manager = new CmsManager();
    CmsPage page = manager.GetPage(new Guid("321b652b-1ff2-446b-a363-2c501cfa5850"), true) as CmsPage;
    page.Staged.AddControl("placeHolder", "~/UserControls/MyControl.ascx");
    page.Publish();


    I couldn't change properties too? Or change values of properties is possible?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Mar 2010
    Link to this post
    Hello Fernando,

    You can change the value of a property, but not add the property. To change the value of a property, once you get the control, create an object and cast it to the control type. Then access the property of your control.

    sample code

    if (control.IsAscx && cntrl.TypeName == "~/Sample.ascx")
    {
        Control cntrl = control.LoadControl(this.Page);
        Sample mycontrol = cntrl as Sample;
        mycontrol.Property = "Hello";
        Manager.SaveControl(control);
     
    }


    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
4 posts, 0 answered