2007年10月14日 星期日

.NET 2.0 TextBox ReadOnly attribute

在.NET 2.0裡面,asp:TextBox的ReadOnly屬性如果被設成true的話(例如: <asp:textbox id="tbx_test" runat="server" />,或是使用tbx_test.ReadOnly = true;),會對前端的javascript造成一點影響,譬如我們常常在前端使用javascrip將textbox的值改變,但是當程式postback到後方以後,會發現textbox所保有的值仍然是舊的值,要想改正這個行為,有幾種方法:

1. 在後端以Request["tbx_test"],或是Request.Form["tbx_test"]來取得改正過後的值,然後用tbx_test.Text = Request["tbx_test"].ToString();在後重新設定textbox的值,

2. 不使用ReadOnly="true"或是tbx_test.ReadOnly = true;,而是在後端使用
tbx_test.Attributes.Add("ReadOnly", "ReadOnly");,或是
tbx_test.Attributes["ReadOnly"] = "ReadOnly";


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. (Quoted from MSDN)

reference: http://darkthread.blogspot.com/2006/11/kb-aspnet-20-textboxreadonly.html

2 則留言:

匿名 提到...

Nice Site!
you can visit my site too

Your full guide to earn money online

匿名 提到...

Nice Site!
you can visit my site too

Your full guide to earn money online