<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/asp-net1-and-net2-textbox-html/</link>
<title><![CDATA[ASP.NET 2.0中只读的TextBox]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Tue, 21 Sep 2010 07:43:03 +0000</pubDate> 
<guid>https://www.heckjj.com/asp-net1-and-net2-textbox-html/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 微软雅黑;">有时候，我们不希望用户直接编辑TextBox，而是希望通过客户端脚本的方式来设置内容，一般的做法是设置TextBox的属性ReadOnly 为true。但在ASP.NET 2.0里有了变化，设置了ReadOnly为true的TextBox，在服务器端不能通过Text属性获取在客户端设置的新内容，在Reflector 里比较一下LoadPostData的实现<br/><br/><span style="font-size: 14px;"><strong><span style="color: #4169E1;">.NET 1.1中:</span></strong></span></span><br/><textarea name="code" class="c#" rows="15" cols="100">
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string text1 = this.Text;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string text2 = postCollection[postDataKey];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!text1.Equals(text2))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Text = text2;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
&#125;
</textarea><br/><span style="font-family: 微软雅黑;"><span style="font-size: 14px;"><strong><span style="color: #4169E1;">.NET 2.0中:</span></strong></span></span><br/><textarea name="code" class="c#" rows="15" cols="100">
protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base.ValidateEvent(postDataKey);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string text1 = this.Text;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string text2 = postCollection[postDataKey];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!this.ReadOnly && !text1.Equals(text2, StringComparison.Ordinal))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.Text = text2;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
&#125;</textarea><br/><span style="font-family: 微软雅黑;"><br/>就可以看出，如果设置了ReadOnly为true，从客户端传回的新的值是不被设置到Text属性的。<br/><br/>想要保持.NET 1.*中的行为，建议的做法是设置客户端属性ContentEditable=false，参考<br/><br/>SYSK 118: ReadOnly or ContentEditable?<br/><a href="http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx" target="_blank">http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx</a><br/><br/>其实如果是设置客户端属性的话，设置客户端的readonly属性应该也是可以的:<br/><br/>TextBox1.Attributes["readonly"] = "true";</span><br/>Tags - <a href="https://www.heckjj.com/tags/.net/" rel="tag">.net</a> , <a href="https://www.heckjj.com/tags/asp.net/" rel="tag">asp.net</a> , <a href="https://www.heckjj.com/tags/textbox/" rel="tag">textbox</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/asp-net1-and-net2-textbox-html/#blogcomment</link>
<title><![CDATA[[评论] ASP.NET 2.0中只读的TextBox]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/asp-net1-and-net2-textbox-html/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>