Microsoft KB Archive/917733

= The validation of a TextBox control may fail when you use a client-side script to update the value of the TextBox control in an ASP.NET 2.0 Web application =

Article ID: 917733

Article Last Modified on 7/14/2006

-

APPLIES TO


 * Microsoft ASP.NET 2.0

-





SYMPTOMS
When you try to update the value of a TextBox control in a Microsoft ASP.NET 2.0 Web application, the validation of the TextBox control may fail. This problem occurs when the following conditions are true:
 * The ReadOnly attribute of the TextBox control is set to true in the control declaration.
 * You use a client-side script to update the value of the TextBox control.



CAUSE
This problem occurs because ASP.NET 2.0 prevents the client computer from changing the value of a TextBox control when the ReadOnly attribute is set to true in the control declaration.



WORKAROUND
To work around this problem, follow these steps:  Delete the ReadOnly attribute from the control declaration. Alternatively, set the ReadOnly attribute to false.  Programmatically set the ReadOnly attribute to true. For Microsoft Visual C#, use the following code example. protected void Page_Load(object sender, EventArgs e)   { this.TextBox1.ReadOnly = true; } For Microsoft Visual Basic .NET, use the following code example. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load TextBox1.ReadOnly = true End Sub 



Steps to reproduce the problem
 Start Microsoft Visual Studio 2005. Click File, point to New, and then click Web Site. Click ASP.NET Web Site, and then click OK.

Note By default, a new Web site that is named WebSite1 is created, and the Default.aspx file opens in Source view.</li>  In the Default.aspx file, replace the existing code with the following code. <%@ Page language=&quot;c#&quot; Inherits=&quot;_Default&quot; CodeFile=&quot;Default.aspx.cs&quot; %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; > <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> WebForm1 <script language=&quot;javascript&quot; type=&quot;text/javascript&quot;> function Button3_onclick { var textBox1 = document.getElementById('TextBox1'); textBox1.value = &quot;Text From Client-side script&quot;; }       <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:TextBox id=&quot;TextBox1&quot; ReadOnly=&quot;true&quot; runat=&quot;server&quot;></asp:TextBox> <asp:RequiredFieldValidator id=&quot;RequiredFieldValidator1&quot; runat=&quot;server&quot; ErrorMessage=&quot;RequiredFieldValidator&quot; ControlToValidate=&quot;TextBox1&quot;></asp:RequiredFieldValidator>

<asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Cause Validation&quot;></asp:Button>

<input id=&quot;Button3&quot; type=&quot;button&quot; value=&quot;Set Text (Client)&quot; onclick=&quot;return Button3_onclick&quot; /> </li> Click Build, and then click Start Debugging.

Note The Web site is compiled, and the Default.aspx file is displayed in a browser window.</li> Click Set Text (Client), and then click Cause Validation.

Note The value of the TextBox control is reset, and the validation fails.</li></ol>

<div class="references_section">