惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
D
Docker
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
IT之家
IT之家
The Last Watchdog
The Last Watchdog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
B
Blog
Recorded Future
Recorded Future
博客园_首页
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
Security Archives - TechRepublic
Security Archives - TechRepublic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hacker News: Front Page
T
Threatpost
H
Heimdal Security Blog
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog

博客园 - Sherrys

Content-Type 参数 Sql 2000 中行转列的查询方法 DotNet Framework 小技巧 通过 INotifyPropertyChanged 实现观察者模式 使用 .NET 2.0 SecureString 类保护敏感数据 API参数说明符前缀详解 C#中调用Windows API的要点 - Sherrys - 博客园 SQL 处理简单的 Xml Special Considerations When Using Query Notifications 利用.NET Framework使用RSS feed SQL远程连接 SQL 事务的隔离 SELECT 语句收藏(2000 & 2005) 自动处理 SQL 2005 表格数据 SQL事务的使用 用JS让文章内容指定的关键字加亮 - Sherrys - 博客园 查询SQL连接数的方法 对象的继承方案 如何利用SQL Server 2005数据库快照形成报表
使用 ICallbackEventHandler aspx页面中的DropDownList 的 SelectValue 出现中文导致不回调方法的问题
Sherrys · 2007-01-27 · via 博客园 - Sherrys

在一次开发过程中,我在页面中使用 ICallbackEventHandler 来做回调方法来显示下拉框省份和城市,刚开始页面就只有这点东西,也没有一点问题,做好之后丢给自己的同事做剩下的页面工作,在这个页面做完之后郁闷的事情发生了,刚开始我写的 ICallbackEventHandler 的方法不回调了,我开始检查是否方法错了,开始做断点。。。一一排除```可是发现写的 ICallbackEventHandler 如果提出来单独运行没有一点问题,可是和页面里面其他代码一起运行的时候就出问题了,很奇怪?难道代码冲突?```貌似没有听说过这样的冲突,只有逻辑出现冲突的```(复杂的错误排查中。。。)

后来问题停留在这段代码上面了:

<asp:DropDownList ID="drpAuditState" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpAuditState_SelectedIndexChanged">
<asp:ListItem Value="全部" Text="全部"></asp:ListItem>
<asp:ListItem Value="待审核" Text="待审核"></asp:ListItem>
<asp:ListItem Value="未通过审核" Text="未通过审核"></asp:ListItem>
<asp:ListItem Value="通过审核" Text="通过审核"></asp:ListItem>
</asp:DropDownList>

把上面这段代码注释的话就没有问题了,但是一加上这段代码就使 ICallbackEventHandler 不能回调服务器端的代码了。。。

后来换成了这样:

<asp:DropDownList ID="drpAuditState" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpAuditState_SelectedIndexChanged">
<asp:ListItem Value="1" Text="全部"></asp:ListItem>
<asp:ListItem Value="2" Text="待审核"></asp:ListItem>
<asp:ListItem Value="3" Text="未通过审核"></asp:ListItem>
<asp:ListItem Value="4" Text="通过审核"></asp:ListItem>
</asp:DropDownList>

这样子就可以正常运行了。

原因找到了,现在粗略的分析一下:

第一:如果在 DropDownList 里面的Value使用中文的话,需要在 Web.Config 里面设置默认的字符编码是

<globalization requestEncoding="utf-8" responseEncoding="utf-8"/> 这不写也是默认的

第二:如果已经在Web.Config里面使用了 <globalization requestEncoding="gb2312" responseEncoding="gb2312"/> 这样出现了上面的情况就会导致 ICallbackEventHandler 不能正常的运行了,不过这个问题可以解决在后台手动添加 DropDownList 里面的值:

drpAuditState.Items.Add(new ListItem("全部",Server.UrlEncode("全部")));

...

...

这里需要使用到 Server.UrlEncode 方法把字符串做一个 Url 编码让它可以被浏览器进行传值的时候认的到。然后取值的时候再做一道解码的工作就可以了,Server. UrlDecode 这样也可以解决问题。

小结:其实大多数的开发过程中不会遇见此问题,只有在控制了网站的字符集为 gb2312 然后使用 DropDownList 的时候又使用到了中文的值才会有这样的问题发生。不过遇到的时候确实还是汗了一下。。。留一个心得在此以后做一个参考。。。