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

Forums / Developing with Sitefinity / Replacing Sitefinity UserControls with Regular UserControls

Replacing Sitefinity UserControls with Regular UserControls

6 posts, 0 answered
  1. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    22 Oct 2008
    Link to this post
    While reviewing the kb article on custom workflow notifications, I noticed that the main step was to make a code-behind file for the controltemplate usercontrol... this led me to wonder, why don't the controls have code-behind? Disabling them cripples a lot of functionality, and prevents us from adding custom code to do certain things simply, and as a result we have to do stuff like extend contentview controls...

    Since the kb article basically suggests to turn this sitefinity control into a regular control, I tried just replacing it, deleting the sitefinity control and replacing it with a regular one, then copy-pasting all the html data, and adding custom code to its code behind. this was completely successful!

    I proceeded to test this with other issues, such as my need to fix the problem of comibining Source and Author in the event module into a single link (as outlined here http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-beehgt.aspx), and adding an additional link for my outlook handler (http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-bgehdd.aspx) and was able to successfuly replace the controls and use the code-behind...

    so why is it that this model isn't encouraged? it's SOOOO much easier, and fits better within the asp.net programming model. I'm sure there's a good reason for this but I'd like to know what it is.
  2. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    22 Oct 2008
    Link to this post

    Why don't the controls have code-behind?

    The "code-behind" for Sitefinity is almost entirely contained in DLL files.  All of this is precompiled.

    It would be possible to have separate code-behind files with raw source code for you to tweak.  However, the upgrade path starts to get really messy in this scenario.  We're already seeing this with the ControlTemplates.  Your customizations will be overwritten with an upgrade and need to be re-applied.

    Here is my personal opinion: I think Sitefinity already has too many files.  A default installation of Sitefinity contains over 3,000 files.  I would personally like to see this number reduced to under 20.  I'm not joking. 

    One option for accomplishing this is to include ALL Sitefinity files in the Sitefinity DLLs.  This means there would be very little you could alter in a default Sitefinity installation.  However, I would like to include the option to export & use external ControlTemplates (and perhaps code-behind?) if you wish to customize parts of Sitefinity. 

    In this scenario your Sitefinity web site would consist of only a handful of Sitefinity DLL files.  Everything else would be your customizations.  Upgrading would simply involve uploading a handful of new Sitefinity DLL files.  When you upgrade, it will be obvious where Sitefinity ends and your own customizations begin.  None of it would be co-mingled.

    What are your thoughts on this scenario?

    So why is it that this model isn't encouraged? it's SOOOO much easier, and fits better within the asp.net programming model. I'm sure there's a good reason for this but I'd like to know what it is.

    You have some golden advice included in your post.  For any Sitefinity ControlTemplate you can configure a code-behind file and include some custom code.  For example, I can include the following line at the beginning of my ~/Sitefinity/Admin/ControlTemplates/Events/ControlPanelInsert.ascx file:

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlPanelInsert.ascx.cs" Inherits="Sitefinity_Admin_ControlTemplates_Events_ControlPanelInsert" %>   
     
     
     
     

     

    Then I can create a ~/Sitefinity/Admin/ControlTemplates/Events/ControlPanelInsert.ascx.cs file with the following contents:

    using System;  
     
    public partial class Sitefinity_Admin_ControlTemplates_Events_ControlPanelInsert : System.Web.UI.UserControl  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            saveButton1.Click += new EventHandler(ButtonClicked);  
        }  
     
        protected void ButtonClicked(object sender, EventArgs e)  
        {  
            Response.Write("Hello");  
        }  
    }  
     
     
     

     

    This allows me to VERY EASILY attach custom code to existing Sitefinity controls.  It's great!  My only issue with this is the upgrade path.  Ideally we keep "your stuff" separate from the "Sitefinity stuff".  All of us are doing a poor job of that right now.  I'm very interested in a ways to solve this without limiting your ability to customize Sitefinity.

    Again, your feedback is welcome.  Let me know your thoughts.

    Gabe Sumner
    http://blogs.telerik.com/GabeSumner/Posts.aspx

  3. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    22 Oct 2008
    Link to this post
    One last thing;

    In my experience UserControls and code-behind files start to break down when you begin implementing custom Control Designers and Type Editors:

    http://www.sitefinity.com/help/developer-manual/controls-adding-controls-type-editors.html

    http://www.sitefinity.com/help/developer-manual/control-adding-controls-designers.html

    For Type Editors, User Controls are unable to inherit from the WebUITypeEditor class.

    For Control Designers, we need to cast the "base.DesignedControl" to make changes to the underlying properties.

    Here is what this looks like:

    1 (MyCustomClass) myClass = (MyCustomClass) base.DesignedControl;  
    2 myClass.myCustomProperty = "hello".  
    3  
    4  


    If we're dealing with only a User Control, how do we reference the UserControl's properties in a strongly typed way?  There isn't a way (that I know of) to use the namespace to reference public properties stored in a User Control.

    I could be wrong about all of this though.  I'm anxious to learn from others if anyone has any tips.

    Gabe Sumner

  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    22 Oct 2008
    Link to this post
    hey I'm just glad I didn't break something again :)

    i think a good idea would be to use the default control template and instantiate it via code instead of a user control. sitefinity can do this right? if the default control template file doesn't exist, it creates a default one that is just the basic of the basic (just the input controls) at run time.I don't know too much about the internals so I could be wrong...

    this could be the default so that if users never use controltemplates and just use the builtin ones won't have to upgrade any user control files when a new release comes out.

    while you're at it the default template could also dynamically render inputs for all the meta fields. you could define the control type (text, date, datetime, list, etc) and the default template would instantiate a corresponding field... that way meta fieds could be added with just one entry in web.config.

    then if we want to use custom templates, we just create the markup for our control, including only the meta fields we need (everything else would be instantiated in the code behind dll compiled into sitefinity core) and code behind to handle events and additional processing.

    this could be even FURTHER enhanced of these internal classes were exposed to us as partial classes, so that we could EXTEND them.

    for example, if I want to handle the Save button click, I'd have to replace the control just as you did in your example, and make sure to manually wire up an event to the physical control that I had to place on the page. If, however, the button was a member of the internal class, you could just allow me in the partial class to add an event handler to the public property encapsulating that control:

    public partial class ControlTemplateInsertControl  
    {  
       protected Page_Load(object sender, EventArgs e)  
       {  
           this.SaveButton.Click += new EventHandler(MyHandler);  
       }     

    of course I don't know TOO much about this stuff so I could be way off the mark...
  5. Nitesh Rastogi
    Nitesh Rastogi avatar
    15 posts
    Registered:
    24 Aug 2009
    04 Sep 2009
    Link to this post
    Hi Gabe,

    My requirement is that i need to create a new modulle, which i have done taking help of developer manual.Know what i want is that give the business user an option to select the no of controls (may be one or two) and based on the selected number dynamically generate a Rad Editor and display on the control panel of the module at that time.Then the user fills the content in the editor and saves the same.

    How do i accomplish this . Can you provide some source for dynamically generating the controls on the control panel of the module.

    Thanks
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    04 Sep 2009
    Link to this post
    Hello Nitesh Rastogi,

    You should create a view. In the template of this view you need to add several LinklButtons. Each Linkbutton should have a CommandName. When pressed the page will make a post back and then a new control will be generated. Then save the control properties. The control should be added to the Controls collection so that you should also save its properties to the databse.

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