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 (917733)



The information in this article 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:
  1. Delete the ReadOnly attribute from the control declaration. Alternatively, set the ReadOnly attribute to false.
  2. 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
    

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio 2005.
  2. Click File, point to New, and then click Web Site.
  3. 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.
  4. In the Default.aspx file, replace the existing code with the following code.
    <%@ Page language="c#" Inherits="_Default" CodeFile="Default.aspx.cs" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<title>WebForm1</title>
    		<script language="javascript" type="text/javascript">
                function Button3_onclick() {
                    var textBox1 = document.getElementById('TextBox1'); 
                    textBox1.value = "Text From Client-side script";
                }
    		</script>
        </head>
    	<body>
    		<form id="Form1" method="post" runat="server">
    			<asp:TextBox id="TextBox1" ReadOnly="true" runat="server"></asp:TextBox>
    			<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    			<br />
    			<asp:Button id="Button1" runat="server" Text="Cause Validation"></asp:Button>
    			<br />
    			<input id="Button3" type="button" value="Set Text (Client)" onclick="return Button3_onclick()" />
    		</form>
    	</body>
    </html>
  5. Click Build, and then click Start Debugging.

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

    Note The value of the TextBox control is reset, and the validation fails.

REFERENCES

For more information about how to use the TextBox control, visit the following Microsoft Developer Network (MSDN) Web site: For more information about how to use the ReadOnly attribute of the TextBox control, visit the following MSDN Web site:

Modification Type:MajorLast Reviewed:7/14/2006
Keywords:kbWebForms kbValidation kbScript kbASP kbinfo kbtshoot kbprb KB917733 kbAudDeveloper