More in this section

Forums / Developing with Sitefinity / update property of control

update property of control

23 posts, 0 answered
  1. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    03 Nov 2009
    Link to this post
    Hi all

    I'm testing some concepts out.

    I have written a control with a text property.
    Uploaded it and added it to a page.
    Can set the property when editing it, works great (The control just displays the value of the property to check it's working)
    I'm trying to change the control now to have a textbox in it and a button so the user puts a new value in and clicks the button. So it can update the property of the control.

    I've tried it with adding a generic content control and it works fine like this (from the documentation)
    Dim gcCtrl As Telerik.Cms.Engine.WebControls.GenericContent = TryCast(webCtrl.LoadControl(), Telerik.Cms.Engine.WebControls.GenericContent) 
    gcCtrl.Content = [String].Concat("[Obsolete] ", gcCtrl.Content) 
    cmsManager.SaveControl(webCtrl) 

    But it's not working with my non Sitefinity control. Trying something like:
    Dim gcCtrl As MyControlClassName = TryCast(webCtrl.LoadControl(),MyControlClassName) 
    gcCtrl.valuestring = "new property value" 
    cmsManager.SaveControl(webCtrl) 
    but this doesn't work :( Not sure if it's the way I'm casting it or the fact that it's a property?

    Any help gratefully received, I'm stuck!

    Thanks

    Matt
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Nov 2009
    Link to this post
    Hi mattc,

    Thank you for using our services.

    Can you, please check if the properties that you are trying to get or set of your custom controls are publicly exposed? If the properties are private, do you have publicly exposed get or set methods? When running this code what kind of error are you getting? Can you provide us with a stack trace?

    Thank you for your cooperation.

    Kind regards,
    Radoslav Georgiev
    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.
  3. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    03 Nov 2009
    Link to this post
    Hi Radoslav

    Thanks for your quick reply.
    Yes the property is public and is working in the Sitefinity page editor:
    Inherits System.Web.UI.UserControl 
        Private _valuestring As String = "" 
     
        Public Property valuestring() As String 
            Get 
                Return _valuestring 
            End Get 
            Set(ByVal value As String) 
                _valuestring = value 
            End Set 
        End Property 
    It's not giving an error, just not doing anything.
    I will get you a stack trace.

    Thanks

    Matt
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Nov 2009
    Link to this post
    Hello mattc,

    Well if you are not getting an error it seems that the control properties are not getting persisted in the View State. For the public set and get methods of our controls we do check the view state if it contains a value for that property:
    public virtual string Content
    {
        get
        {
            string str = (string) this.ViewState["Content"];
            if (str != null)
            {
                return str;
            }
            return string.Empty;
        }
        set
        {
            this.ViewState["Content"] = value;
            this.contentId = null;
            this.sharedContent = null;
        }
    }


    Sincerely yours,
    Radoslav Georgiev
    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.
  5. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    03 Nov 2009
    Link to this post
    Hi Radoslav

    No the viewstate works fine on the control, the updates I make in the control when running it are persisted through postbacks, just not persisted to the control property..

    Am I missing something obvious here!?

    Thanks

    Matt

  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Nov 2009
    Link to this post
    Hello mattc,

    You are not missing something obvious. When you add controls dynamically you should define whether control properties should be persisted. In the case of our controls we are using the following to define whether a control property is persistable:
    [CmsBrowsable(false), CmsPersistable(true)]
    public virtual string Content
    {
        get
        {
           ...
        }
        set
        {
            ...
        }
    }

    Can you try to see whether adding this will work on your control? If it does not then you should define a way to persist control properties in a db table.

    Kind regards,
    Radoslav Georgiev
    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.
  7. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    03 Nov 2009
    Link to this post
    Hi Radoslav

    Thanks for your reply.
    I tried that but that's not the problem. The control is already on the page, I'm just trying to change the value of the property on it.

    I want to be able to change this property value on the control in the Sitefinity administration are (so I want the property to be browsable) but I also want to be able to do it from the page itself..

    Thanks

    Matt
  8. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    03 Nov 2009
    Link to this post
    Hi again

    I'm getting lost in this now!
    Do my controls have to implement System.Web.UI.UserControl
    or should they be implementing a Telerik class? That part of the online API manual doesn't show in my browser.

    Matt
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Nov 2009
    Link to this post
    Hello mattc,

    If you are building custom control are .cs files located in the ~/App_Code the controls should inherit from System.Web.UI.WebControls.WebControl. If you are creating Web User Controls (.ascx with code files) they automatically inherit from System.Web.UI.UserControl) you do not have to make them inherit from one of our classes.

    All the best,
    Radoslav Georgiev
    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.
  10. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    03 Nov 2009
    Link to this post
    Hi
    I'm just creating normal user controls (.ascx) files.
    Still can't work out why this is not working then!
  11. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Nov 2009
    Link to this post
    Hi mattc,

    When you are adding the user control, can you try to use this method:
    ICmsPage.Staged.AddControl(controlcontainer, "~/controlpath", controilID);

    Are you following the steps for adding controls outlined in our defined in our developer manual - Creating Controls?

    Greetings,
    Radoslav Georgiev
    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.
  12. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    04 Nov 2009
    Link to this post
    Hi Radoslav

    Thanks for sticking with me on this one! :)
    I'm not adding the control programatically at the moment, it's already in the page (added through the sitefinity manager).
    All I'm trying to do is update the property on the control from the control itself, a bit like the example on your site at http://www.sitefinity.com/help/developer-manual/controls-update.html
    Only real difference is I'm using a created control and trying to update a different property as far as I'm aware?

    Thanks


  13. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Nov 2009
    Link to this post
    Hello mattc,

    Thank you for the further information.

    Once you are done modifying the control, you should publish the page. Please take a look at the following sample taken from one of our webinars - Sitefinity API tips & tricks:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Telerik.Cms;
    using Telerik.Cms.Engine.WebControls;
     
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CmsManager manager = new CmsManager();
            IList pages = manager.GetPages();
     
            foreach (IPage page in pages)
            {
                if (page.Name == "TestPage")
                {
                    Response.Write("Updating page.<br />");
     
                    ICmsPage editPage = (ICmsPage)manager.GetPage(page.ID, true);
                    IStagedPage staged = editPage.Staged.CheckOut();
                    IList<ICmsWebControl> pageControls = editPage.Staged.Controls;
     
                    // Modify an existing control
                    foreach (ICmsWebControl control in pageControls)
                    {
                        if (control.ControlType == typeof(GenericContent))
                        {
                            GenericContent gc = (GenericContent)control.LoadControl();
                            if (gc.ID == "genericContent1")
                            {
                                gc.Content = "<p>Altered text.</p>";
                                manager.SaveControl(control);
                            }
                        }
                    }
     
                    // Add a new control
                    GenericContent control3 = new GenericContent();
                    control3.ID = "genericContent3";
                    control3.Content = "<p>This is some content #3.</p>";
                    editPage = (ICmsPage)manager.GetPage(page.ID, true);
                    editPage.Staged.AddControl("Content", control3);
     
                    editPage = (ICmsPage)manager.GetPage(page.ID, true);
                    editPage.Staged.CheckIn();
                    editPage.Publish();
     
                    break;
                }
            }
        }
    }

    Every time the control collection of a page has been modified you should save the page changes.

    Regards,
    Radoslav Georgiev
    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.
  14. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    04 Nov 2009
    Link to this post
    Hi Radoslav

    OK, I looked at that too but still no luck! :(
    Here is my user control:
    Imports Telerik.Web 
     
    Partial Class myTestControl 
     
        Inherits System.Web.UI.UserControl 
        Private _valuestring As String 
     
        <CmsPersistable(True)> _ 
        Public Property valuestring() As String 
            Get 
                Return _valuestring 
            End Get 
            Set(ByVal value As String) 
                _valuestring = value 
            End Set 
        End Property 
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
            Response.Write(_valuestring) 
        End Sub 
    End Class 

    I've just added the response.write to see what the property value is without introducing any other controls.

    I then created an external page with the following code:
    Imports Telerik.Cms 
    Imports System.Collections.Generic 
    Imports Telerik.Web 
     
    Partial Class APItest 
        Inherits System.Web.UI.Page 
     
       Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click 
     
            'page guid to edit 5a151482-b146-4ae5-af0b-429ae3e94a97 
     
            Dim cmsManager As New Telerik.Cms.CmsManager() 
            Dim pageGUID As New System.Guid("5a151482-b146-4ae5-af0b-429ae3e94a97") 
     
            'get the page 
            Dim pageFromDb As Telerik.Cms.ICmsPage = cmsManager.GetPage(pageGUID, True) 
     
            'get the control holder 
            Dim objControl As ICmsWebControl = pageFromDb.Staged.Controls(0) 
     
            'get the inner control 
            Dim objInnerControl As MyTestControl = TryCast(objControl.LoadControl(), MyTestControl) 
     
            'set the value of the inner control property 
            objInnerControl.valuestring = "set from API!" 
     
            'save the control 
            cmsManager.SaveControl(objControl) 
     
            'get page again to check in 
            Dim getPageAgain As ICmsPage = cmsManager.GetPage(pageGUID, True) 
            getPageAgain.Staged.CheckIn() 
            getPageAgain.Publish() 
     
        End Sub 
    End Class 

    It seems odd that I don't have to save objInnerControl but this is what the examples seem to do?
    Can you see why this doesn't work?

    Matt
  15. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Nov 2009
    Link to this post
    Hi mattc,

    I am really sorry for taking so long to figure this out.

    Now I remember that we have had problems with this and user controls. The issue appears to be somewhere in the implementation of the manager so I am not able to provide any other workaround but to try to remove the old version of the control and then add the edited instance.

    I should apologize for taking so long to see what was really happening.

    Regards,
    Radoslav Georgiev
    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.
  16. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    04 Nov 2009
    Link to this post
    Hi Radoslav

    You are right! I changed my code to get the original control, delete the object then create another and it works.

    Not very ideal though! Will have to think about it. Are there any implications of deleting and recreating the control each time I want to change a property?

    Thanks

    Matt
  17. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    04 Nov 2009
    Link to this post
    Hello mattc,

    Generally this will decrease the performance because instead of just adding  you are doing removing and adding.

    Kind regards,
    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.
  18. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    04 Nov 2009
    Link to this post
    Hi Ivan
    Thanks for getting back to me.
    It's a shame this does not work well. Was planning on building a lot of stuff into control properties but suppose I should look at storing stuff in other places now.

    So when you edit properties in the Sitefinity manager is it deleting and creating the controls too? Does the Sitefinity manager not use the same API?

    Thanks

    Matt
  19. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    05 Nov 2009
    Link to this post
    Hi guys

    I'm trying to get a workable solution on this as if Sitefinity is going to be a valid platform for our needs we would have to be able to update things on peoples pages from code using the API.

    So I'm looking at ideas like taking the original control and cloning it (to preserve the properties) then deleting the original, adding the clone again and saving the page.

    Any suggestions?
    Are there ways like not using user controls?

    This seems to be a rather basic need for a site, are there plans to fix it in V4?

    Thanks

    Matt

  20. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Nov 2009
    Link to this post
    Hello mattc,

    Thank you for getting back to us.

    A better alternative to using user controls would be to build custom distributable controls. Make them inherit our interface for this ICmsWebControl and pretty much follow the same design principles as with our controls. Although this approach would prove a little bit more time consuming it will save you the headaches that you get from using user controls. It will also allow you greater versatility and easier reuse of these controls over different Sitefinity projects.

    A good learning resource to start using custom controls is the following webinar - Creating Highly Usable & Distributable Sitefinity Controls Webinar

    Regards,
    Radoslav Georgiev
    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.
  21. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    05 Nov 2009
    Link to this post
    Hi Radoslav

    Ok thanks, will look at that. Will that achieve both:
    1: Being able to edit properties of the control through the API?
    2: If I upload a new DLL with new functionality/UI all instances of the control will update?

    Thanks

    Matt
  22. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Nov 2009
    Link to this post
    Hi mattc,

    The answer to both questions should be yes. After all our controls are distributable controls.  I can recommend that you take a look at our countrols with .NET Reflector to see how we implement them.

    Kind regards,
    Radoslav Georgiev
    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.
  23. mattc
    mattc avatar
    105 posts
    Registered:
    11 Aug 2007
    06 Nov 2009
    Link to this post
    Hi Radoslav, Ivan

    OK, to finish this thread off.
    I built a very simple Custom Control to prove the concept and I'm happy to say that both requirements are met, ie, I can access an existing instance of the control on a page using the API, change a property and save it and it updates fine while still being editable in the Sitefinity admin interface as expected.

    Also, I can then change the HTML output and the structure of the class (ie add another property), rebuild it and all existing controls in the site will update to the new content while maintaining their saved values.
    All good stuff.

    So I guess I will build the functionality in Custom Controls for the new application. It is a bit more work although will benefit from the ease of deployment etc etc.

    It would still be good if the standard User Controls behaved themselves when edited through the API as it means that any controls in the site that need to be edited through the API have to be Custom Controls. Would also be good to document this weakness, hopefully anyone else looking at this will find this thread.

    Thanks for all the help getting to the bottom of it, I'm getting to love Sitefinity!

    Matt
Register for webinar
23 posts, 0 answered