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

Forums / Developing with Sitefinity / User Control that creates two controls programmatically

User Control that creates two controls programmatically

13 posts, 0 answered
  1. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    24 Nov 2009
    Link to this post
    Hello!

    So, i lost myself a little bit here, so another route was chosen.

    If i add a control programmatically, i can edit it normally in Page Edit Mode. I saw this article and it was a bit helpful in terms of understanding the code necessary to create them.

    Now what i'm asking is how i can relate a User Control to two controls declared programmatically? Is there an Event that can be executed when the User Control is placed in a container (in a similar way that i can put two controls in a page when i create a page, following the KB article i indicated above)?

    Or what is the way to have two controls declared programmatically inside a User Control?

    Thanks in advance,
    Ricardo Marcelino
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    24 Nov 2009
    Link to this post
    Hi Ricardo Marcelino,

    In the code behind of your first control you can add two different controls dynamically so that when you drop this control it will create the other two.

    example:

    public class CustomControl: SimpleControl
    {
        public CustomControl()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        protected override void CreateChildControls()
        {
     
            Label label = new Label();
            label.Text = "Hello";
            Controls.Add(label);
     
            NewsView news = new NewsView();
            Controls.Add(news);
     
        }
    }


    Sincerely yours,
    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.
  3. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    25 Nov 2009
    Link to this post
    So, since my control is named mgTesting.ascx, my mgTesting.ascx.cs whould be:

    public partial class mgTesting : SimpleControl 
        public mgTesting() 
        { 
     
        } 
     
        protected override void CreateChildControls() 
        { 
            GenericContent genCont = new GenericContent(); 
            genCont.Content = "Hello"
            Controls.Add(genCont); 
     
            SitefinityImage img = new SitefinityImage(); 
            Controls.Add(img); 
        } 
     
        protected void Page_Load(object sender, EventArgs e) 
        {         
     
        }    

    Right now they aren't appearing when i drop my mgTesting while editing a page.

    [EDIT] Ok, i placed CreateChildControls() in my public class CustomControlBase : System.Web.UI.UserControl. I managed to create only one control, but it's not displayed as i would want, since i want to be able to edit the properties, like i was editing a drag-n-dropped Sitefinity Image Control, or a GenericContent control for that matter. I don't want to personalize that edit screen, i want to use the default ones.

    As i said, i managed to create controls programmatically, with the end-result that i wanted, but when defining templates, following the KB article i indicated. When a control is placed in a container, Events are triggered. I need to know which events they are, so that in my code-behind of my User Control, i can use that Event (most likely InitializeComponent ?) and apply the creation of the controls to that Event.

    I hope i'm being explicit. And please bear with me, if i'm complicating or even missing a technical point.

    Thanks in advance,
    Ricardo Marcelino
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    25 Nov 2009
    Link to this post
    Hi Ricardo Marcelino,

    You have to use a custom control not a user control. You have created a user control and change the class from which you are inheriting. As you see I am creating a public class ( you can create it in App_Code or in a Class Library and compile it) that inherits from SimpleControl class ( you can inherit from CompositeControl as well)

    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.
  5. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    25 Nov 2009
    Link to this post
    Hey!

    Okey, i've got my custom control and it's displaying the controls i added programmatically. I want to be able to edit their properties while editing a page (binding the context of the page to the controls, "simulating" a single drag-n-drop of a, for example, GenericContent Control?). How can i do that?

    Thanks for your patience,
    Ricardo Marcelino
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    25 Nov 2009
    Link to this post
    Hi Ricardo Marcelino,

    You can create a public properties in the custom control of the same Type as your controls or just expose some public properties that will do some of your logic.

    Greetings,
    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.
  7. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    27 Nov 2009
    Link to this post
    Hello!

    When a custom control of mine is dropped in a page, it triggers Events inside Sitefinity right? Which events are they? How can i access them and place some code of mine in there?

    And how can i grab the page where the same custom control was dropped into?

    Thank you in advance,
    Ricardo Marcelino

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    27 Nov 2009
    Link to this post
    Hi Ricardo Marcelino,

    Which event you want to modify? Generally you can access only events from your control. To get the current page you can use the code below:

    var Manager = new CmsManager();
    ICmsUrlContext pageID = (ICmsUrlContext)this.Context.Items[Telerik.Cms.Web.UrlHelper.CmsPageKey];
            if (pageID != Guid.Empty)
            {
                Guid ID = pageID.PageID;
                ICmsPage cmsPage = (ICmsPage)Manager.GetPage(ID);
            }

    Best wishes,
    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.
  9. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    27 Nov 2009
    Link to this post
    Hey Ivan,

    An event like Load would do it. When it loads into the page, it runs the code i want (in my case, adding controls to the page where the custom control was dropped, through CmsManager).

    Thank you,
    Ricardo Marcelino


  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    27 Nov 2009
    Link to this post
    Hi Ricardo Marcelino,

    Please take a look at Control Execution Lifecycle, CompositeControl Events, ASP.NET Page Life Cycle Overview

    Greetings,
    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.
  11. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    27 Nov 2009
    Link to this post
    Hey Ivan,

    Isn't there one Event that runs just when my custom control is dropped in the page? I want my code to run only once and after the custom control is dropped.

    Thank you,
    Ricardo Marcelino
  12. Ricardo Marcelino
    Ricardo Marcelino avatar
    15 posts
    Registered:
    04 Nov 2009
    02 Dec 2009
    Link to this post
    Hello Ivan,

    Following the code you gave me about getting the the current page, can you show me how to get the template i'm editing/working on? The idea is to determine if a control is on a template or on a page.

    Thank you in advance,
    Ricardo Marcelino
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    04 Dec 2009
    Link to this post
    Hello Ricardo Marcelino,

    Below is a sample code that illustrates desired behavior.

    CmsManager cmanager = new CmsManager();
            ICmsUrlContext pageIDs = (ICmsUrlContext)this.Context.Items[Telerik.Cms.Web.UrlHelper.CmsPageKey];
            if (pageIDs != null)
            {
                bool found = false;
                Guid ID = pageIDs.PageID;
                ICmsPage p = cmanager.GetPage(ID) as ICmsPage;
                if (p.Controls.Count > 0)
                {
                    foreach (ICmsWebControl c in p.Controls)
                    {
                        if (c.IsAscx && c.ID.ToString() == "5ce9f0ce-9a5d-43dc-bf83-9f022566d9d2")
                        {
                            found = true;
                            Response.Write("found in the cms page");
                        }
                    }
                }
                if (!found)
                {
                    IPageTemplate temp = p.Template;
                    if (temp.Controls.Count > 0)
                    {
                        foreach (ICmsWebControl c in temp.Controls)
                        {
                            if (c.IsAscx && c.ID.ToString() == "5ce9f0ce-9a5d-43dc-bf83-9f022566d9d2")
                            {
                                Response.Write("found in the page template");
     
                            }
                        }
                    }
                }
            }


    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.
Register for webinar
13 posts, 0 answered