aspx如何过滤非法字符 例如 '\,.等字符。有很多的文本框需要过滤。请问该怎么处理? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 通过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 spacestext = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tagstext = text.Replace("'", "''");return text;}项目中的web.config文件加上这句话:<httpHandlers><!--过滤提交给服务器的文本信息--><add verb="*" path="*.aspx" validate="false" type="JianCaiWeb.Utils.FilterStrFactoryHandler, JianCaiWeb.Utils"/></httpHandlers>代码其实挺好理解,就是在提交数据的时候(requestType=="POST"),通过PageHandlerFactory找到页面实例,将过滤字符串的方法(FilterStrFactoryHandler_PreLoad)加到Page实例的PreLoad事件上,使用这个方法有一个前提,就是Input和Textarea控件必须作为服务器控件运行,如果不这样做的话,就不能通过页面实例的FindControl方法找到相应的控件。 asp.net中读取Access连接字符串问题 |M| 用Sql触发器更新的时候,如何来判断更新的数据符不符合要求 treeView控件在部份机器上显示不正常 !!!!一个进程的疑惑问题!!!!!!1 如何把我请求的网页直接保存到服务器,而不是输出到客户端浏览器? 怎样判断RadioButtonList什么都没选? 有关用javascript回传值.... 用ASP.net/C#如何从Active Directory服务器读取帐户 缓冲最久可以是多少秒?是否越久越好? 关于quickstats 一个问题。。。。。。。 100分求解,服务器内存使用率将不下来,.程序导致? 关于易宝支付问题
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"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
}项目中的web.config文件加上这句话:
<httpHandlers>
<!--过滤提交给服务器的文本信息-->
<add verb="*" path="*.aspx" validate="false" type="JianCaiWeb.Utils.FilterStrFactoryHandler, JianCaiWeb.Utils"/>
</httpHandlers>代码其实挺好理解,就是在提交数据的时候(requestType=="POST"),通过PageHandlerFactory找到页面实例,将过滤字符串的方法(FilterStrFactoryHandler_PreLoad)加到Page实例的PreLoad事件上,使用这个方法有一个前提,就是Input和Textarea控件必须作为服务器控件运行,如果不这样做的话,就不能通过页面实例的FindControl方法找到相应的控件。