Return to Snippet

Revision: 41043
at February 11, 2011 04:38 by nivlam


Initial Code
public static class UserControlHelper
{
	public static HtmlString RenderControl<T>(this HtmlHelper helper, string controlPath)
		where T : UserControl
	{
		return RenderControl<T>(helper, controlPath, null);
	}

	public static HtmlString RenderControl<T>(this HtmlHelper helper, string controlPath, Action<T> action) 
		where T : UserControl
	{
		Page pageHolder = new Page();
		T controlToRender = (T)pageHolder.LoadControl(controlPath);
		pageHolder.Controls.Add(controlToRender);

		if (action != null)
			action(controlToRender);

		using (StringWriter sw = new StringWriter())
		{
			HttpContext.Current.Server.Execute(pageHolder, sw, false);
			return new HtmlString(sw.ToString());
		}
	}
}

Initial URL


Initial Description


Initial Title
Use ASCX controls in MVC

Initial Tags


Initial Language
C#