All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用javascript轻松制作一套简单的抽奖系统_JavaScript技术_编程技术
.NET:InTheHand提供的类操作SMS_.net资料_编程技术
温故知新ASP.NET,2.0(C#)(6),-,Membership&RoleManager_[Asp.Net教程]
delphi设置代码模板
6种ASP.NET跨页面传值的方法_[Asp.Net教程]
delphi图像列表组件(TImageList)使用实例
通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法_.net资料_编程技术
数据类型的表达式转换为另一种数据类型_[SQL Server教程]
ADO.NET和LINQ中古怪的事务特性_.net资料_编程技术
支付宝Payto接口的c#.net实现_[Asp.Net教程]
Reading number is top 10 pictures
青春清纯美女大集合1
girl of HuNan name is LiXiang(湖南辣姐李湘的写真)
Flow chart of breast implants
擦地板的大叔太好了
The real super beauty4
西游四格漫画(二)
China's programmers are live what kind, had a look at will know that
中国文革时期的色情图片2
9.3阅兵全景图8-航空梯队
Compared GDP and per capita income in China for 40 years
Download software ranking
Jinling thirteen stock
少妇苏霞全本
Tram sex maniac 2 (H) rar bag12
SP4 for SQL2000
Boxer's Top ten classic battle2
Boxer Classic video3
Be there or be square
C++教程第四版
WebService在.NET中的实战应用教学视频 → 第3集
Unix video tutorial4
归海一刀 published in(发表于) 2014/1/30 1:13:56 Edit(编辑)
通过IHttpHandlerFactory,过滤TextBox、Input和Textarea中的字符_[Asp.Net教程]

通过IHttpHandlerFactory,过滤TextBox、Input和Textarea中的字符_[Asp.Net教程]

通过IHttpHandlerFactory,过滤TextBox、Input和Textarea中的字符_[Asp.Net教程]

通过IHttpHandlerFactory,过滤TextBox、Input和Textarea中的特殊字符


通过IHttpHandlerFactory过滤特殊字符,可以做到和具体项目无关,部署起来也挺简单。
using System;
using System.IO;
using System.Web.UI;
using System.Web;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web.Compilation;
using System.Reflection;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace JianCaiWeb.Utils
{
public class FilterStrFactoryHandler : IHttpHandlerFactory
{
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
//得到编译实例(通过反射)
PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory),true);
IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
//过滤字符串
if (requestType == "POST")
{
Page page = handler as Page;
if (page != null)
page.PreLoad += new EventHandler(FilterStrFactoryHandler_PreLoad);
}


//返回
return handler;
}
//过滤TextBox、Input和Textarea中的特殊字符
void FilterStrFactoryHandler_PreLoad(object sender, EventArgs e)
{
try
{
Page page = sender as Page;
NameValueCollection postData = page.Request.Form;
foreach (string postKey in postData)
{
Control ctl = page.FindControl(postKey);
if (ctl as TextBox != null)
{
((TextBox)ctl).Text = Common.InputText(((TextBox)ctl).Text);
continue;
}
if (ctl as HtmlInputControl != null)
{
((HtmlInputControl)ctl).Value = Common.InputText(((HtmlInputControl)ctl).Value);
continue;
}
if (ctl as HtmlTextArea != null)
{
((HtmlTextArea)ctl).Value = Common.InputText(((HtmlTextArea)ctl).Value);
continue;
}
}
}
catch { }
}


public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}


Common.InputText的代码为:
//字符串过滤
public static string InputText(string text)
{
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //

text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //&nbsp;
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
}项目中的web.config文件加上这句话:




代码其实挺好理解,就是在提交数据的时候(requestType=="POST"),通过PageHandlerFactory找到页面实例,将过滤字符串的方法(FilterStrFactoryHandler_PreLoad)加到Page实例的PreLoad事件上,使用这个方法有一个前提,就是Input和Textarea控件必须作为服务器控件运行,如果不这样做的话,就不能通过页面实例的FindControl方法找到相应的控件。


来源:http://www.cnblogs.com/fengfeng







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.