Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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.

↑ Back to the top


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
    

↑ Back to the top


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.

↑ Back to the top


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:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB917733, kbprb, kbtshoot, kbinfo, kbasp, kbscript, kbvalidation, kbwebforms

↑ Back to the top

Article Info
Article ID : 917733
Revision : 3
Created on : 7/14/2006
Published on : 7/14/2006
Exists online : False
Views : 453