using System.Collections;
using System.Web;
using System.Web.Compilation;
using System.Web.SessionState;
using System.Web.UI;
namespace ZSoft.Web.Controller
{
class AspNet1ExtendHandler : IHttpHandler, IRequiresSessionState
{
IHttpHandler 成员#region IHttpHandler 成员
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
string url = context.Request.Path;
IHttpHandler h = PageParser.GetCompiledPageInstance(url,context.Server.MapPath(url),context);
(h as Page).PreRender += new EventHandler(ctx_PreRender);
context.Handler = h;
// 交由Asp.net默认的Handler处理
h.ProcessRequest(context);
}
#endregion
private void ctx_PreRender(object sender, EventArgs e)
{
Extend(sender as Page);
}
private void Extend(System.Web.UI.Control control)
{
string script = GetAttribute(control,"OnClientClick");
if(script != null)
{
AddAttribute(control,"onclick",script);
}
foreach(Control child in control.Controls)
{
Extend(child);
}
}
private string GetAttribute(Control control,string keyword)
{
if(control is WebControl)
{
return (control as WebControl).Attributes[keyword];
}
if (control is HtmlControl)
{
return (control as HtmlControl).Attributes[keyword];
}
}
private void AddAttribute(Control control, string key, string value)
{
string oldValue = GetAttribute(control, key);
if (oldValue != null)
{
if (oldValue.IndexOf(value) != -1) oldValue = oldValue.Remove(oldValue.IndexOf(value), value.Length);
if (!oldValue.EndsWith(";") && oldValue != "") oldValue += ";";
}
value = oldValue + value;
if (control != null)
{
if (control is WebControl)
{
WebControl webCtrl = control as WebControl;
webCtrl.Attributes.Add(key, value);
}
if (control is HtmlControl)
{
HtmlControl htmlCtrl = control as HtmlControl;
htmlCtrl.Attributes.Add(key, value);
}
}
}
}
}
来源:http://www.cnblogs.com/northdevil