asp.net 解决中文乱码问题

一、解决中文乱码问题,在 web.config 中加入指定配置节即可~~~

同样该方法将统一设置每个页面的编码为  gb2312 (取决于下面 web.config 的配置)

加入位置:在   </system.web> 上面添加

如下所示:

   ....................
  <system.web>

       <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" responseHeaderEncoding="gb2312" fileEncoding="gb2312" />

  </system.web>

 

二、设置单个页面的默认编码

 

1.在页面的 head 标签中加入 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 

2.在 webform 的 page_load  函数中 加入 this.Context.Response.Charset = "UTF8";

 

这样,页面在加载后默认的编码就是 utf-8 格式了

 

三、接收 使用 utf-8 post 过来的中文解析乱码的问题

 

在你接收请求的函数中 加入 context.Request.ContentEncoding = Encoding.UTF8; 代码,

 

之后在使用 string s = context.Request.Form[0]; 代码接收数据,应该不乱码了!

 

注:必须遵守的规则就是 context.Request.ContentEncoding 的设置必须要与提交请求端使用相同编码!!

posted @ 2012-09-10 14:27  草青工作室  阅读(739)  评论(0编辑  收藏  举报