How to load custom Web User Controls

2 posts, 0 answers
  1. Thomas
    Thomas avatar
    1 posts
    Member since:
    Jul 2010

    Posted 12 Nov 2014 Link to this post


    I have the following situation: I have multiple self-developed Web User Controls (ascx) in my web application. The user controls are holding different logic, for example a rather complex contact form.

    Every page's content on the website, is edited throud RadEditor. Now I need a flexible way to dynamically load page content including one, two, three, x of my own custom user controls....

    How can i accomplish this in a neat way with the RadEditor?

    My own solution is, in the RadEditor's contentarea, to insert a reference to the user control like %%control:MyFancyUserControl%%, and then at runtime search for %%control:MyFancyUserControl%% and load the control when the page is rendered.
  2. Marin Bratanov
    Marin Bratanov avatar
    3558 posts

    Posted 14 Nov 2014 Link to this post

    Hello Thomas,

    RadEditor for ASP.NET AJAX is an XHTML WYSIWIG editor and not a webforms/ASP editor like Visual Studio. With this in mind, you cannot load a user control in it in order to edit it and republish it. From what I can understand you need a content management system, so you can consider the Telerik SItefinity product.

    What you can load in the editor is HTML, so you can have the user control  render to a dummy container if this fits your needs. Here is an example that you can use as base to render user controls to HTML in order to use that HTML:

    Page pageHolder = new Page();
    UserControl viewControl = (UserControl)pageHolder.LoadControl("~/WebUserControl.ascx");
    StringWriter output = new StringWriter();
    HttpContext.Current.Server.Execute(pageHolder, output, false);
    RadEditor1.Content = output.ToString();

    With user control like this:

    some literal text
    <br />
    <asp:Panel ID="Panel1" runat="server">
            some paragraph in the asp Panel (div)
    <asp:Label ID="Label1" Text="a label control" runat="server" />

    you will get the following HTML in the editor:

    some literal text
    <br />
    <div id="ctl00_Panel1">
    some paragraph in the asp Panel (div)
    <span id="ctl00_Label1">a label control</span>

    Note that, in order to use form elements like textboxes or buttons, you should take some additional steps to have a form on the page used to render the user controls. Also, this is just one example, there are other ways to do this, and what is important is that neither approach offers a two-way conversion OOB, which means that when you edit the HTML in RadEditor, there is no built- in facility to translate it back into ASP markup.


    Marin Bratanov

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

  3. DevCraft R3 2016 release webinar banner
Back to Top