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

推荐订阅源

Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 司徒正美
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
小众软件
小众软件
T
Threatpost
Latest news
Latest news
J
Java Code Geeks
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Project Zero
Project Zero
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
S
Schneier on Security
T
The Blog of Author Tim Ferriss
V
V2EX
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
罗磊的独立博客
IT之家
IT之家
雷峰网
雷峰网
H
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
I
InfoQ
GbyAI
GbyAI
博客园 - 叶小钗
PCI Perspectives
PCI Perspectives
The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
H
Heimdal Security Blog
Spread Privacy
Spread Privacy
博客园_首页
A
About on SuperTechFans
T
Tailwind CSS Blog
The Register - Security
The Register - Security

博客园 - 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 的时候又使用到了中文的值才会有这样的问题发生。不过遇到的时候确实还是汗了一下。。。留一个心得在此以后做一个参考。。。