UserControlExtensions.cs :  » Content-Management-Systems-CMS » Kooboo » System » Web » Mvc » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » System » Web » Mvc » UserControlExtensions.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.IO;
using System.Collections.Specialized;
using System.Linq.Expressions;
using System.Data;
using System.Web.UI;
namespace System.Web.Mvc{

    [global::System.Serializable]
    public class RenderViewControlException : Exception
    {
        public RenderViewControlException() { }
        public RenderViewControlException(string message) : base(message) { }
        public RenderViewControlException(string message, Exception inner) : base(message, inner) { }
        protected RenderViewControlException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    public static class UserControlExtensions
    {

        #region User Controls

        #region Helper Methods

        /// <summary>
        /// Renders the specified ViewUserControl to a string
        /// </summary>
        /// <param name="virtualPath">The virtual path to the control</param>
        /// <returns>System.String</returns>
        public static string RenderUserControl(this ViewPage viewPage, string virtualPath)
        {
            return RenderUserControl(viewPage, virtualPath, null);
        }

        /// <summary>
        /// Renders the specified ViewUserControl to a string
        /// </summary>
        /// <param name="viewPage">The view page.</param>
        /// <param name="virtualPath">The virtual path to the control</param>
        /// <param name="propertySettings">Property settings for the control. Use Anonymous Typing for this: new{Name="MVC"}</param>
        /// <returns>System.String</returns>
        public static string RenderUserControl(this ViewPage viewPage, string virtualPath, object propertySettings)
        {
            virtualPath = VirtualPathUtility.Combine(viewPage.AppRelativeTemplateSourceDirectory, virtualPath);
            return viewPage.Html.RenderUserControl(virtualPath, propertySettings);
        }

        /// <summary>
        /// Instances a ViewUserControl located at the supplied virtual path
        /// </summary>
        /// <param name="virtualPath">the virtual path to the control</param>
        /// <returns>ViewUserControl</returns>
        static UserControl InstanceControl(string virtualPath)
        {

            Type ctrlType;
            UserControl instance = null;

            try
            {
                //check for the control, using the path as the key

                ctrlType = BuildManager.GetCompiledType(virtualPath);

                ConstructorInfo ctor = ctrlType.GetConstructor(new Type[] { });
                instance = (UserControl)ctor.Invoke(new object[] { });

            }
            catch (Exception x)
            {
                throw new RenderViewControlException("Unable to instance the ViewUserControl: " + x.Message, x);

            }
            return instance;
        }

        /// <summary>
        /// Creates a ViewUserPage class to hold the ViewUserControl for rendering
        /// </summary>
        /// <param name="instance">The instance of the ViewUserControl</param>
        /// <returns>HtmlExtensionUtility.CustomPage</returns>
        static HtmlExtensionUtility.CustomPage GetPageForControl(UserControl instance, ViewContext viewContext)
        {
            HtmlExtensionUtility.CustomPage dummyPage = new HtmlExtensionUtility.CustomPage();
            dummyPage.ViewContext = viewContext;
            dummyPage.InitHelpers();
            dummyPage.Controls.Add(instance);
            return dummyPage;
        }


        /// <summary>
        /// Sets any properties on the control
        /// </summary>
        /// <param name="instance">The ViewUserControl instance</param>
        /// <param name="propertySettings"></param>
        static void SetUserControlProperties(UserControl instance, object propertySettings)
        {
            if (propertySettings != null)
            {
                Hashtable props = HtmlExtensionUtility.GetPropertyHash(propertySettings);
                SetUserControlProperties(instance, props);
            }
        }
        static void SetUserControlProperties(UserControl instance, Hashtable propertySettings)
        {
            //property setter bits            
            try
            {
                if (propertySettings != null)
                {

                    HtmlExtensionUtility.SetPropsFromHash(instance, propertySettings);
                }
            }
            catch (Exception x)
            {
                throw new RenderViewControlException("Error setting properties for the ViewUserControl: " + x.Message, x);

            }
        }

        #endregion


        #region Render Methods

        /// <summary>
        /// Renders the specified ViewUserControl to a string
        /// </summary>
        /// <param name="virtualPath">The virtual path to the control</param>
        /// <returns>System.String</returns>
        public static string RenderUserControl(this HtmlHelper helper, string virtualPath)
        {
            return RenderUserControl(helper, virtualPath, null);
        }

        /// <summary>
        /// Renders the user control.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="propertySettings">The property settings.</param>
        /// <returns></returns>
        public static string RenderUserControl(this HtmlHelper helper, string virtualPath, object propertySettings)
        {
            Hashtable props = null;
            if (propertySettings != null)
            {
                props = HtmlExtensionUtility.GetPropertyHash(propertySettings);
            }
            return helper.RenderUserControl(virtualPath, props);
        }

        /// <summary>
        /// Renders the specified ViewUserControl to a string
        /// </summary>
        /// <param name="virtualPath">The virtual path to the control</param>
        /// <param name="controlData">The data to send to the control as ViewData</param>
        /// <param name="propertySettings">Property settings for the control. Use Anonymous Typing for this: new{Name="MVC"}</param>
        /// <returns>System.String</returns>
        public static string RenderUserControl(this HtmlHelper helper, string virtualPath, Hashtable propertySettings)
        {
            //instance control
            UserControl instance = InstanceControl(virtualPath);

            HtmlExtensionUtility.CustomPage dummyPage = GetPageForControl(instance, helper.ViewContext);

            //pass it the default context from the helper
            //PassContextToUserControl(helper, dummyPage, instance);

            //set the properties
            SetUserControlProperties(instance, propertySettings);

            //if (controlData == null)
            //{
            //    SetUserControlData(helper, instance, helper.ViewContext.ViewData);
            //}
            //else
            //{

            //    SetUserControlData(helper, instance, controlData);

            //}

            //Render it
            string result = HtmlExtensionUtility.RenderPage(dummyPage);

            return result;

        }


        #endregion

        #endregion

    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.