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

推荐订阅源

V
Vulnerabilities – Threatpost
aimingoo的专栏
aimingoo的专栏
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
IT之家
IT之家
V
Visual Studio Blog
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
A
About on SuperTechFans
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
N
News and Events Feed by Topic
A
Arctic Wolf
WordPress大学
WordPress大学
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
T
Threat Research - Cisco Blogs
Latest news
Latest news
Simon Willison's Weblog
Simon Willison's Weblog
Cyberwarzone
Cyberwarzone
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
Lohrmann on Cybersecurity
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
J
Java Code Geeks
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
I
Intezer
L
LangChain Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
G
GRAHAM CLULEY
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

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