|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web;
using System.Web.UI.HtmlControls;
namespace ControlLoading
{
internal class IncrementalLoaderHandler : IHttpHandler
{
private StringBuilder _controlBuilder;
private StringWriter _controlWriter;
private HtmlTextWriter _htmlWriter;
#region IHttpHandler Members
/// <summary>
/// Accepts incoming HTTP requests and executes the resource specified in the querystring
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpContext"></see> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
public void ProcessRequest(HttpContext context)
{
// *** Simulate latency ***
Random r = new Random();
int wait = r.Next(500, 3000);
System.Threading.Thread.Sleep(wait);
if (context.Request.QueryString[IncrementalLoader.ControlQuery] != null)
{
try
{
// Create a page instance to host the user control
Page page = new Page();
// ** Alternative for rendering controls that need a <form> tag
//HtmlForm form = new HtmlForm();
string controlLocation = Utility.DecryptString(context.Request.QueryString[IncrementalLoader.ControlQuery]);
UserControl control = (UserControl)page.LoadControl(controlLocation);
page.Controls.Add(control);
// ** Alternative for rendering controls that need a <form> tag
//page.Controls.Add(form);
//form.Controls.Add(control);
//form.SetRenderMethodDelegate(new RenderMethod(RenderForm));
// Execute the page and return the output (only the control's output is returned)
StringWriter output = new StringWriter();
context.Server.Execute(page, output, false);
// ** Alternative for rendering controls that need a <form> tag
//((IHttpHandler)page).ProcessRequest(context);
context.Response.Clear();
context.Response.Write(output.ToString());
// ** Alternative for rendering controls that need a <form> tag
//context.Response.Write(_controlWriter.ToString());
}
catch { }
}
}
/// <summary>
/// Renders the control.
/// </summary>
/// <param name="hw">The hw.</param>
/// <param name="ctrl">The CTRL.</param>
protected void RenderForm(HtmlTextWriter writer, Control control)
{
_controlBuilder = new StringBuilder();
_controlWriter = new StringWriter(_controlBuilder);
_htmlWriter = new HtmlTextWriter(_controlWriter);
foreach (Control child in control.Controls)
{
if (!(child is ScriptManager))
{
child.RenderControl(_htmlWriter);
}
}
}
/// <summary>
/// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"></see> instance.
/// </summary>
/// <value></value>
/// <returns>true if the <see cref="T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
public bool IsReusable
{
get { return true; }
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm the founder of PageLabs, a web-based performance and SEO optimization site.
Give your site a boost in performance, even take a free speed test!
http://www.pagelabs.com