/*
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
}
}
|