ASP.NET: TextBox控件如果设置为ReadOnly则PostBack之后可能丢失数据的问题

这是在项目中发现的一个小问题,TextBox控件应该是设计使然。有关的解决方法是,不要直接设置ReadOnly=true,而是通过Javascript的方式为其添加readonly的attribute。例如下面这样做

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script src="jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
    <link href="themes/cupertino/ui.all.css" rel="stylesheet" type="text/css" />
    <script src="ui/ui.core.js" type="text/javascript"></script>
    <script src="ui/ui.datepicker.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            $("input.datepicker").attr("readonly", "readonly").datepicker();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="date" runat="server" CssClass="datepicker"></asp:TextBox>
    <asp:Button ID="bt" runat="server" Text="测试" />
    </div>
    </form>
</body>
</html>
 
原文地址:https://www.cnblogs.com/chenxizhang/p/1663074.html