asp.net 2.0里当readonly遇上enableviewstate=false
偶然在一个老外的blog里看到有这样的描述,当textbox控件里被设置为readonly时,而且页面的enableviewsate设置为false时,提交后,textbox的值会丢失,这只发生在asp.net 2.0中,在asp.net 1.0/1.1中不会出现这样的情况,代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" enableviewstate="false" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
protected void page_load(object sender, eventargs e)
{
if (!this.ispostback)
{
this.textbox1.text = "readonly text";
}
}
protected void button1_click(object sender, eventargs e)
{
this.lblmessage.text = this.textbox1.text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form runat="server" id="form1">
<asp:textbox id="textbox1" runat="server" readonly="true" forecolor="silver"></asp:textbox>
<asp:textbox id="textbox2" runat="server" readonly="true">some text</asp:textbox>
<asp:button id="button1" runat="server" text="button" onclick="button1_click" /><br />
<asp:label id="lblmessage" runat="server" text="label"></asp:label>
</form>
</body>
</html>
在.net 2.0下运行,的确会丢失了文本框的值。最后,找到了msdn的解析和微软的bug反馈中心,其实这不是bug,是.net 2.0下为了安全的一个小改变,具体摘录如下,大家就明白了:
微软的反馈为:
after careful analysis, the explanation for the observed behaviour is that:
with a design change in asp .net based on user security concern, the input for a readonly textbox is saved in viewstate, which doesnt happen if viewstate is disabled. to workaround this, a page developer can add the readonly attribute to the textbox.attributes collection, which can then be used to access the value of the textbox.
we hope this clarifies. thank you.
web server & tools
msdn 2005的解析:
the text value of a textbox control with the readonly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. this prevents a malicious user from changing a text value that is read-only. the value of the text property is preserved in the view state between postbacks unless modified by server-side code.
jackyrong blog
- · C# 2.0与泛型
- · 让3721也无奈的弹出窗口(代码)
- · 玩透9种网页弹出窗口(精)
- · 网站左右两边浮动广告JS代码
- · 一个IP只提示一次设为首页的代码
- · 常用ASP脚本程序集锦*精(适合初学者)
- · 鼠标自动移动/点击
- · PHP程序加速探索之加速工具软件
- · SQLServer2000数据访问基类
- · 图解MySQL数据库的安装和操作
- · 一些ASP初学者常用的代码
- · ASP经典问答收藏之一
- · 一段防注入的通用脚本
- · 简单的防盗链(代码)
- · PHP窜红:革命尚未成功 Java仍需努力
- · 使用PHP编写基于Web的文件管理系统
- · 理解PHP中的MVC编程之控制器
- · 理解PHP中的MVC编程之MVC框架简介
- · SQL Server Express 数据库自动部署问题及解决
- · 用PHP文件上传的具体思路及实现
- · 回顾与展望PHP 5.0的变化与PHP 6.0展望
- · 一个产生中文累计数的代码片断
- · 在SQL Server 2005中解决死锁
- · 30分钟正则表达式指导
- · 不算不知道 44% 数据库开发者使用MySQL
- · 立即释放.net下的com组件
- · XHTML的目标,规则和细节
- · SQL Server 2005 提供的分页查询支持
- · ASP.NET程序中常用的三十三种代码
- · Sql server存储过程和C#分页类简化你的代码
- · SQL Server 2005新功能-TSQL
- · 在SQL Server 2005中编辑SQL Server 2000 DTS
- · .NET 连接到 Oracle的oci.dll加载错误解决方案
- · 如何在调用线程的时候传递参数
- · 专家预言:PHP将比Java更好更受欢迎
- · 在IIS6.0下ASP .NET 的版本冲突问题
- · 解决SqlTransaction用尽的问题(SQL处理超时)
- · 以前编写Like谓词被忽略的使用方法
- · 在编写存储过程时使用 Set NoCount On
- · ASP.NET 2.0运行时简要分析
- · .Net中如何操作IIS(原理篇)
- · 用.net开发不同操作系统下应用的winform的size大小问题
- · SQL Server数据库文件恢复技术
- · SQL SERVER中一些常见性能问题的总结
- · .NET下对二进制文件进行加密解密(C#)
- · 利用.NET的File控件上传文件的最终解决方案(个人版)
- · 如何把图片、声音等存储到sql中
- · MS-SQL server数据库开发精典技巧
- · 全文索引—CONTAINS语法
- · 获得所有表信息的SQL语句
- · .NET扫描远程计算机注册表
- · 利用JS获取IE客户端IP及MAC的实现
- · 简单而又复杂的ASP.NET编程模型
- · C#2.0终于有了?:便捷判断的单分支版
- · SQL Server 2005 Data Mining简介
- · ASP.NET控件开发之
- · ASP.NET中用哪种方式表格化数据
- · .NET平台下几种SOCKET模型的简要性能供参考
- · 使用 XML 模板 (MSSQL手册)
- · 使用带批注的 XDR 架构创建 XML 视图

